rev-parse: have --parseopt callers exit 0 on --help

The standard philosophy for Unix software when a help option (such as --help) is specified is that the software should exit 0, printing the help output to standard output, since the standard output is for user-requested output and the program performed the requested task successfully. If the user specifies an incorrect option, then the help output should be printed to standard error (since the user has made a mistake) and it should exit unsuccessfully. git rev-parse --parseopt properly directs the output in both of these cases, but it currently exits 129 when it receives a --help or -h option on the command line, which causes its invoking script to do the same. This is not in line with the usual behavior and it causes scripts using this command to exit unsuccessfully on --help as well. Note that Git subcommands implemented using scripts, such as git submodule, don't have this problem because Git itself intercepts the --help option and runs man (or a similar tool), which then exits 0. However, this still affects the myriad scripts that use this functionality because Git is widespread and the --parseopt functionality is a good way to get sensible option parsing across shells in a portable way. Because git rev-parse --parseopt is intended to be eval'd by the shell, when help output is to be printed to standard output, Git actually prints a cat command with a heredoc since the standard output is being evaluated by the shell. Thus, to do the right thing, simply add an "exit 0" right after the end of the heredoc, which will cause the invoking program to exit successfully. The usual invocation recommended by the manual page is this: eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" Thus, the fact that git rev-parse --parseopt still exits 129 in this case is irrelevant, since the "echo exit $?" will print "exit 129", but that will be after the "exit 0" printed by Git—and thus ignored, since the shell will have already exited successfully. Update the tests for this case. Note that we no longer need to delete only the first and last lines in some tests, so add a command to delete the end of the heredoc as well. We could do something clever with sed to delete all but the last two lines or switch to head and tail, but those would be more complicated and less readable, so just stick with the simple approach. In t1517, add three shell scripts to the failure case because they no longer return 129 as expected. In a future commit, we'll change the expected result to exit 0 and these will become successful again. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jul 8, 2026 at 00:15 UTC 06f9b9501afcdf2f64c1d9a2fce964da824fe054
5 files changed +13 -6
parse-options.c
+1 -1
@@ -1479,7 +1479,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1479 fputc('\n', outfile);
1480
1481 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1482 - fputs("EOF\n", outfile);
1482 + fputs("EOF\nexit 0\n", outfile);
1483
1484 return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP;
1485 }
t/t1502-rev-parse-parseopt.sh
+7 -2
@@ -12,7 +12,7 @@ check_invalid_long_option () {
12 cat <<-\EOF &&
13 error: unknown option `'${opt#--}\''
14 EOF
15 - sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/$spec.help"
15 + sed -e 1d -e /EOF/d -e \$d <"$TEST_DIRECTORY/t1502/$spec.help"
16 } >expect &&
17 test_expect_code 129 git rev-parse --parseopt -- $opt \
18 2>output <"$TEST_DIRECTORY/t1502/$spec" &&
@@ -87,6 +87,7 @@ test_expect_success 'test --parseopt help output no switches' '
87 | some-command does foo and bar!
88 |
89 |EOF
90 +|exit 0
91 END_EXPECT
92 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
93 test_cmp expect output
@@ -100,6 +101,7 @@ test_expect_success 'test --parseopt help output hidden switches' '
101 | some-command does foo and bar!
102 |
103 |EOF
104 +|exit 0
105 END_EXPECT
106 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
107 test_cmp expect output
@@ -115,6 +117,7 @@ test_expect_success 'test --parseopt help-all output hidden switches' '
117 | --[no-]hidden1 A hidden switch
118 |
119 |EOF
120 +|exit 0
121 END_EXPECT
122 test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
123 test_cmp expect output
@@ -125,7 +128,7 @@ test_expect_success 'test --parseopt invalid switch help output' '
128 cat <<-\EOF &&
129 error: unknown option `does-not-exist'\''
130 EOF
128 - sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
131 + sed -e 1d -e /EOF/d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
132 } >expect &&
133 test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
134 test_cmp expect output
@@ -252,6 +255,7 @@ test_expect_success 'test --parseopt help output: "wrapped" options normal "or:"
255 | -h, --help show the help
256 |
257 |EOF
258 + |exit 0
259 END_EXPECT
260
261 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
@@ -289,6 +293,7 @@ test_expect_success 'test --parseopt help output: multi-line blurb after empty l
293 | -h, --help show the help
294 |
295 |EOF
296 + |exit 0
297 END_EXPECT
298
299 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
t/t1502/optionspec-neg.help
+1
@@ -10,3 +10,4 @@ usage: some-command [options] <args>...
10 --no-negative cannot be positivated
11
12 EOF
13 +exit 0
t/t1502/optionspec.help
+1
@@ -34,3 +34,4 @@ Extras
34 --[no-]extra1 line above used to cause a segfault but no longer does
35
36 EOF
37 +exit 0
t/t1517-outside-repo.sh
+3 -3
@@ -132,10 +132,10 @@ do
132 difftool--helper | filter-branch | format-rev | fsck-objects | \
133 get-tar-commit-id | \
134 gui | gui--askpass | \
135 - http-backend | http-fetch | http-push | init-db | \
135 + http-backend | http-fetch | http-push | init-db | instaweb | \
136 merge-octopus | merge-one-file | merge-resolve | mergetool | \
137 - mktag | p4 | p4.py | pickaxe | remote-ftp | remote-ftps | \
138 - remote-http | remote-https | replay | send-email | \
137 + mktag | p4 | p4.py | pickaxe | quiltimport | remote-ftp | remote-ftps | \
138 + remote-http | remote-https | replay | request-pull | send-email | \
139 sh-i18n--envsubst | shell | show | stage | submodule | svn | \
140 upload-archive--writer | upload-pack | web--browse | whatchanged)
141 expect_outcome=expect_failure ;;