submodule foreach: fix "<command> --quiet" not being respected

Robin reported that

    git submodule foreach --quiet git pull --quiet origin

is not really quiet anymore [1]. "git pull" behaves as if --quiet is not
given.

This happens because parseopt in submodule--helper will try to parse
both --quiet options as if they are foreach's options, not git-pull's.
The parsed options are removed from the command line. So when we do
pull later, we execute just this

    git pull origin

When calling submodule helper, adding "--" in front of "git pull" will
stop parseopt for parsing options that do not really belong to
submodule--helper foreach.

PARSE_OPT_KEEP_UNKNOWN is removed as a safety measure. parseopt should
never see unknown options or something has gone wrong. There are also
a couple usage string update while I'm looking at them.

While at it, I also add "--" to other subcommands that pass "$@" to
submodule--helper. "$@" in these cases are paths and less likely to be
--something-like-this. But the point still stands, git-submodule has
parsed and classified what are options, what are paths. submodule--helper
should never consider paths passed by git-submodule to be options even
if they look like one.

The test case is also contributed by Robin.

[1] it should be quiet before fc1b9243cd (submodule: port submodule
    subcommand 'foreach' from shell to C, 2018-05-10) because parseopt
    can't accidentally eat options then.

Reported-by: Robin H. Johnson <robbat2@gentoo.org>
Tested-by: Robin H. Johnson <robbat2@gentoo.org>
Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2019-04-12 17:08:19 +07:00 committed by Junio C Hamano
parent aeb582a983
commit a282f5a906
3 changed files with 20 additions and 9 deletions

View File

@ -566,12 +566,12 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
}; };
const char *const git_submodule_helper_usage[] = { const char *const git_submodule_helper_usage[] = {
N_("git submodule--helper foreach [--quiet] [--recursive] <command>"), N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
NULL NULL
}; };
argc = parse_options(argc, argv, prefix, module_foreach_options, argc = parse_options(argc, argv, prefix, module_foreach_options,
git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN); git_submodule_helper_usage, 0);
if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0) if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
return 1; return 1;
@ -709,7 +709,7 @@ static int module_init(int argc, const char **argv, const char *prefix)
}; };
const char *const git_submodule_helper_usage[] = { const char *const git_submodule_helper_usage[] = {
N_("git submodule--helper init [<path>]"), N_("git submodule--helper init [<options>] [<path>]"),
NULL NULL
}; };
@ -2097,7 +2097,7 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
}; };
const char *const git_submodule_helper_usage[] = { const char *const git_submodule_helper_usage[] = {
N_("git submodule--helper embed-git-dir [<path>...]"), N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
NULL NULL
}; };

View File

@ -345,7 +345,7 @@ cmd_foreach()
shift shift
done done
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@" git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
} }
# #
@ -376,7 +376,7 @@ cmd_init()
shift shift
done done
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@" git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
} }
# #
@ -412,7 +412,7 @@ cmd_deinit()
shift shift
done done
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} "$@" git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} -- "$@"
} }
is_tip_reachable () ( is_tip_reachable () (
@ -541,6 +541,7 @@ cmd_update()
${depth:+--depth "$depth"} \ ${depth:+--depth "$depth"} \
$recommend_shallow \ $recommend_shallow \
$jobs \ $jobs \
-- \
"$@" || echo "#unmatched" $? "$@" || echo "#unmatched" $?
} | { } | {
err= err=
@ -933,7 +934,7 @@ cmd_status()
shift shift
done done
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@" git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
} }
# #
# Sync remote urls for submodules # Sync remote urls for submodules
@ -966,7 +967,7 @@ cmd_sync()
esac esac
done done
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@" git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
} }
cmd_absorbgitdirs() cmd_absorbgitdirs()

View File

@ -411,4 +411,14 @@ test_expect_success 'multi-argument command passed to foreach is not shell-evalu
test_cmp expected 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_done test_done