Skip to content

Commit 35b96d1

Browse files
stefanbellergitster
authored andcommitted
builtin/reset: add --recurse-submodules switch
git-reset is yet another working tree manipulator, which should be taught about submodules. When a user uses git-reset and requests to recurse into submodules, this will reset the submodules to the object name as recorded in the superproject, detaching the HEADs. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f2d4899 commit 35b96d1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

builtin/reset.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@
2121
#include "parse-options.h"
2222
#include "unpack-trees.h"
2323
#include "cache-tree.h"
24+
#include "submodule.h"
25+
#include "submodule-config.h"
26+
27+
static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
28+
29+
static int option_parse_recurse_submodules(const struct option *opt,
30+
const char *arg, int unset)
31+
{
32+
if (unset) {
33+
recurse_submodules = RECURSE_SUBMODULES_OFF;
34+
return 0;
35+
}
36+
if (arg)
37+
recurse_submodules =
38+
parse_update_recurse_submodules_arg(opt->long_name,
39+
arg);
40+
else
41+
recurse_submodules = RECURSE_SUBMODULES_ON;
42+
43+
return 0;
44+
}
2445

2546
static const char * const git_reset_usage[] = {
2647
N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
@@ -283,6 +304,9 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
283304
N_("reset HEAD, index and working tree"), MERGE),
284305
OPT_SET_INT(0, "keep", &reset_type,
285306
N_("reset HEAD but keep local changes"), KEEP),
307+
{ OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules,
308+
"reset", "control recursive updating of submodules",
309+
PARSE_OPT_OPTARG, option_parse_recurse_submodules },
286310
OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
287311
OPT_BOOL('N', "intent-to-add", &intent_to_add,
288312
N_("record only the fact that removed paths will be added later")),
@@ -295,6 +319,12 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
295319
PARSE_OPT_KEEP_DASHDASH);
296320
parse_args(&pathspec, argv, prefix, patch_mode, &rev);
297321

322+
if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT) {
323+
gitmodules_config();
324+
git_config(submodule_config, NULL);
325+
set_config_update_recurse_submodules(RECURSE_SUBMODULES_ON);
326+
}
327+
298328
unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", oid.hash);
299329
if (unborn) {
300330
/* reset on unborn branch: treat as reset to empty tree */

t/t7112-reset-submodule.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ test_description='reset can handle submodules'
55
. ./test-lib.sh
66
. "$TEST_DIRECTORY"/lib-submodule-update.sh
77

8+
KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED=1
9+
KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1
10+
KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1
11+
12+
test_submodule_switch_recursing "git reset --recurse-submodules --keep"
13+
14+
test_submodule_forced_switch_recursing "git reset --hard --recurse-submodules"
15+
816
test_submodule_switch "git reset --keep"
917

1018
test_submodule_switch "git reset --merge"

0 commit comments

Comments
 (0)