Skip to content

Commit a282f5a

Browse files
pcloudsgitster
authored andcommitted
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 fc1b924 (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 <[email protected]> Tested-by: Robin H. Johnson <[email protected]> Signed-off-by: Robin H. Johnson <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeb582a commit a282f5a

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

builtin/submodule--helper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,12 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
566566
};
567567

568568
const char *const git_submodule_helper_usage[] = {
569-
N_("git submodule--helper foreach [--quiet] [--recursive] <command>"),
569+
N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
570570
NULL
571571
};
572572

573573
argc = parse_options(argc, argv, prefix, module_foreach_options,
574-
git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN);
574+
git_submodule_helper_usage, 0);
575575

576576
if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
577577
return 1;
@@ -709,7 +709,7 @@ static int module_init(int argc, const char **argv, const char *prefix)
709709
};
710710

711711
const char *const git_submodule_helper_usage[] = {
712-
N_("git submodule--helper init [<path>]"),
712+
N_("git submodule--helper init [<options>] [<path>]"),
713713
NULL
714714
};
715715

@@ -2097,7 +2097,7 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
20972097
};
20982098

20992099
const char *const git_submodule_helper_usage[] = {
2100-
N_("git submodule--helper embed-git-dir [<path>...]"),
2100+
N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
21012101
NULL
21022102
};
21032103

git-submodule.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ cmd_foreach()
345345
shift
346346
done
347347

348-
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
348+
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
349349
}
350350

351351
#
@@ -376,7 +376,7 @@ cmd_init()
376376
shift
377377
done
378378

379-
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@"
379+
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
380380
}
381381

382382
#
@@ -412,7 +412,7 @@ cmd_deinit()
412412
shift
413413
done
414414

415-
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} "$@"
415+
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} -- "$@"
416416
}
417417

418418
is_tip_reachable () (
@@ -541,6 +541,7 @@ cmd_update()
541541
${depth:+--depth "$depth"} \
542542
$recommend_shallow \
543543
$jobs \
544+
-- \
544545
"$@" || echo "#unmatched" $?
545546
} | {
546547
err=
@@ -933,7 +934,7 @@ cmd_status()
933934
shift
934935
done
935936

936-
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@"
937+
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
937938
}
938939
#
939940
# Sync remote urls for submodules
@@ -966,7 +967,7 @@ cmd_sync()
966967
esac
967968
done
968969

969-
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
970+
git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
970971
}
971972

972973
cmd_absorbgitdirs()

t/t7407-submodule-foreach.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,14 @@ test_expect_success 'multi-argument command passed to foreach is not shell-evalu
411411
test_cmp expected actual
412412
'
413413

414+
test_expect_success 'option-like arguments passed to foreach commands are not lost' '
415+
(
416+
cd super &&
417+
git submodule foreach "echo be --quiet" > ../expected &&
418+
git submodule foreach echo be --quiet > ../actual
419+
) &&
420+
grep -sq -e "--quiet" expected &&
421+
test_cmp expected actual
422+
'
423+
414424
test_done

0 commit comments

Comments
 (0)