Files
git-mirror/t/t1517-outside-repo.sh
D. Ben Knoble 129b3632f3 builtin: also setup gently for --help-all
Git experts often check the help summary of a command to make sure they
spell options right when suggesting advice to colleagues. Further, they
might check hidden options when responding to queries about deprecated
options like git-rebase(1)'s "preserve merges" option. But some commands
don't support "--help-all" outside of a git directory. Running (for
example)

    git rebase --help-all

outside a directory fails in "setup_git_directory", erroring with the
localized form of

    fatal: not a git repository (or any of the parent directories): .git

Like 99caeed05d (Let 'git <command> -h' show usage without a git dir,
2009-11-09), we want to show the "--help-all" output even without a git
dir. Make "--help-all" where we expect "-h" to mean
"setup_git_directory_gently", and interpose early in the natural place
("show_usage_with_options_if_asked").

Do the same for usage callers with show_usage_if_asked.

The exception is merge-recursive, whose help block doesn't use newer
APIs.

Best-viewed-with: --ignore-space-change
Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-08-08 11:13:12 -07:00

143 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
test_description='check random commands outside repo'
. ./test-lib.sh
test_expect_success 'set up a non-repo directory and test file' '
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
) &&
test_write_lines one two three four >nums &&
git add nums &&
cp nums nums.old &&
test_write_lines five >>nums &&
git diff >sample.patch
'
test_expect_success 'compute a patch-id outside repository (uses SHA-1)' '
nongit env GIT_DEFAULT_HASH=sha1 \
git patch-id <sample.patch >patch-id.expect &&
nongit \
git patch-id <sample.patch >patch-id.actual &&
test_cmp patch-id.expect patch-id.actual
'
test_expect_success 'hash-object outside repository (uses SHA-1)' '
nongit env GIT_DEFAULT_HASH=sha1 \
git hash-object --stdin <sample.patch >hash.expect &&
nongit \
git hash-object --stdin <sample.patch >hash.actual &&
test_cmp hash.expect hash.actual
'
test_expect_success 'apply a patch outside repository' '
(
cd non-repo &&
cp ../nums.old nums &&
git apply ../sample.patch
) &&
test_cmp nums non-repo/nums
'
test_expect_success 'grep outside repository' '
git grep --cached two >expect &&
(
cd non-repo &&
cp ../nums.old nums &&
git grep --no-index two >../actual
) &&
test_cmp expect actual
'
test_expect_success 'imap-send outside repository' '
test_config_global imap.host imaps://localhost &&
test_config_global imap.folder Drafts &&
echo nothing to send >expect &&
test_must_fail git imap-send -v </dev/null 2>actual &&
test_cmp expect actual &&
(
cd non-repo &&
test_must_fail git imap-send -v </dev/null 2>../actual
) &&
test_cmp expect actual
'
test_expect_success 'check-ref-format outside repository' '
git check-ref-format --branch refs/heads/xyzzy >expect &&
nongit git check-ref-format --branch refs/heads/xyzzy >actual &&
test_cmp expect actual
'
test_expect_success 'diff outside repository' '
echo one >one &&
echo two >two &&
test_must_fail git diff --no-index one two >expect.raw &&
(
cd non-repo &&
cp ../one . &&
cp ../two . &&
test_must_fail git diff one two >../actual.raw
) &&
# outside repository diff falls back to SHA-1 but
# GIT_DEFAULT_HASH may be set to sha256 on the in-repo side.
sed -e "/^index /d" expect.raw >expect &&
sed -e "/^index /d" actual.raw >actual &&
test_cmp expect actual
'
test_expect_success 'stripspace outside repository' '
nongit git stripspace -s </dev/null
'
test_expect_success LIBCURL 'remote-http outside repository' '
test_must_fail git remote-http 2>actual &&
test_grep "^error: remote-curl" actual &&
(
cd non-repo &&
test_must_fail git remote-http 2>../actual
) &&
test_grep "^error: remote-curl" actual
'
for cmd in $(git --list-cmds=main)
do
cmd=${cmd%.*} # strip .sh, .perl, etc.
case "$cmd" in
archimport | cvsexportcommit | cvsimport | cvsserver | daemon | \
difftool--helper | filter-branch | fsck-objects | get-tar-commit-id | \
http-backend | http-fetch | http-push | init-db | \
merge-octopus | merge-one-file | merge-resolve | mergetool | \
mktag | p4 | p4.py | pickaxe | remote-ftp | remote-ftps | \
remote-http | remote-https | replay | send-email | \
sh-i18n--envsubst | shell | show | stage | submodule | svn | \
upload-archive--writer | upload-pack | web--browse | whatchanged)
expect_outcome=expect_failure ;;
*)
expect_outcome=expect_success ;;
esac
case "$cmd" in
instaweb)
prereq=PERL ;;
*)
prereq= ;;
esac
test_$expect_outcome $prereq "'git $cmd -h' outside a repository" '
test_expect_code 129 nongit git $cmd -h >usage &&
test_grep "[Uu]sage: git $cmd " usage
'
test_$expect_outcome $prereq "'git $cmd --help-all' outside a repository" '
test_expect_code 129 nongit git $cmd --help-all >usage &&
test_grep "[Uu]sage: git $cmd " usage
'
done
test_done