Skip to content

Commit 93481a6

Browse files
bmwillgitster
authored andcommitted
submodule--helper: add push-check subcommand
Add the 'push-check' subcommand to submodule--helper which is used to check if the provided remote and refspec can be used as part of a push operation in the submodule. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c19ae47 commit 93481a6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

builtin/submodule--helper.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,50 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
11051105
return 0;
11061106
}
11071107

1108+
static int push_check(int argc, const char **argv, const char *prefix)
1109+
{
1110+
struct remote *remote;
1111+
1112+
if (argc < 2)
1113+
die("submodule--helper push-check requires at least 1 argument");
1114+
1115+
/*
1116+
* The remote must be configured.
1117+
* This is to avoid pushing to the exact same URL as the parent.
1118+
*/
1119+
remote = pushremote_get(argv[1]);
1120+
if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1121+
die("remote '%s' not configured", argv[1]);
1122+
1123+
/* Check the refspec */
1124+
if (argc > 2) {
1125+
int i, refspec_nr = argc - 2;
1126+
struct ref *local_refs = get_local_heads();
1127+
struct refspec *refspec = parse_push_refspec(refspec_nr,
1128+
argv + 2);
1129+
1130+
for (i = 0; i < refspec_nr; i++) {
1131+
struct refspec *rs = refspec + i;
1132+
1133+
if (rs->pattern || rs->matching)
1134+
continue;
1135+
1136+
/*
1137+
* LHS must match a single ref
1138+
* NEEDSWORK: add logic to special case 'HEAD' once
1139+
* working with submodules in a detached head state
1140+
* ceases to be the norm.
1141+
*/
1142+
if (count_refspec_match(rs->src, local_refs, NULL) != 1)
1143+
die("src refspec '%s' must name a ref",
1144+
rs->src);
1145+
}
1146+
free_refspec(refspec_nr, refspec);
1147+
}
1148+
1149+
return 0;
1150+
}
1151+
11081152
static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
11091153
{
11101154
int i;
@@ -1170,6 +1214,7 @@ static struct cmd_struct commands[] = {
11701214
{"resolve-relative-url-test", resolve_relative_url_test, 0},
11711215
{"init", module_init, SUPPORT_SUPER_PREFIX},
11721216
{"remote-branch", resolve_remote_submodule_branch, 0},
1217+
{"push-check", push_check, 0},
11731218
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
11741219
{"is-active", is_active, 0},
11751220
};

0 commit comments

Comments
 (0)