submodule.c: submodule_move_head works with broken submodules

Early on in submodule_move_head just after the check if the submodule is initialized, we need to check if the submodule is populated correctly. If the submodule is initialized but doesn't look like it is populated, this is a red flag and can indicate multiple sorts of failures: (1) The submodule may be recorded at an object name, that is missing. (2) The submodule '.git' file link may be broken and it is not pointing at a repository. In both cases we want to complain to the user in the non-forced mode, and in the forced mode ignoring the old state and just moving the submodule into its new state with a fixed '.git' file link. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Apr 18, 2017 at 14:37 UTC f2d48994dc11ba367e92c38c8025c3354418145a
2 files changed +44 -7
submodule.c
+24 -4
@@ -1332,10 +1332,24 @@ int submodule_move_head(const char *path,
1332 int ret = 0;
1333 struct child_process cp = CHILD_PROCESS_INIT;
1334 const struct submodule *sub;
1335 + int *error_code_ptr, error_code;
1336
1337 if (!is_submodule_initialized(path))
1338 return 0;
1339
1340 + if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1341 + /*
1342 + * Pass non NULL pointer to is_submodule_populated_gently
1343 + * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
1344 + * to fixup the submodule in the force case later.
1345 + */
1346 + error_code_ptr = &error_code;
1347 + else
1348 + error_code_ptr = NULL;
1349 +
1350 + if (old && !is_submodule_populated_gently(path, error_code_ptr))
1351 + return 0;
1352 +
1353 sub = submodule_from_path(null_sha1, path);
1354
1355 if (!sub)
@@ -1353,15 +1367,21 @@ int submodule_move_head(const char *path,
1367 absorb_git_dir_into_superproject("", path,
1368 ABSORB_GITDIR_RECURSE_SUBMODULES);
1369 } else {
1356 - struct strbuf sb = STRBUF_INIT;
1357 - strbuf_addf(&sb, "%s/modules/%s",
1370 + char *gitdir = xstrfmt("%s/modules/%s",
1371 get_git_common_dir(), sub->name);
1359 - connect_work_tree_and_git_dir(path, sb.buf);
1360 - strbuf_release(&sb);
1372 + connect_work_tree_and_git_dir(path, gitdir);
1373 + free(gitdir);
1374
1375 /* make sure the index is clean as well */
1376 submodule_reset_index(path);
1377 }
1378 +
1379 + if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1380 + char *gitdir = xstrfmt("%s/modules/%s",
1381 + get_git_common_dir(), sub->name);
1382 + connect_work_tree_and_git_dir(path, gitdir);
1383 + free(gitdir);
1384 + }
1385 }
1386
1387 prepare_submodule_repo_env_no_git_dir(&cp.env_array);
t/lib-submodule-update.sh
+20 -3
@@ -1213,14 +1213,31 @@ test_submodule_forced_switch_recursing () {
1213 )
1214 '
1215 # Updating a submodule from an invalid sha1 updates
1216 - test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
1216 + test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1217 prolog &&
1218 reset_work_tree_to_interested invalid_sub1 &&
1219 (
1220 cd submodule_update &&
1221 git branch -t valid_sub1 origin/valid_sub1 &&
1222 - test_must_fail $command valid_sub1 &&
1223 - test_superproject_content origin/invalid_sub1
1222 + $command valid_sub1 &&
1223 + test_superproject_content origin/valid_sub1 &&
1224 + test_submodule_content sub1 origin/valid_sub1
1225 + )
1226 + '
1227 +
1228 + # Old versions of Git were buggy writing the .git link file
1229 + # (e.g. before f8eaa0ba98b and then moving the superproject repo
1230 + # whose submodules contained absolute paths)
1231 + test_expect_success "$command: updating submodules fixes .git links" '
1232 + prolog &&
1233 + reset_work_tree_to_interested add_sub1 &&
1234 + (
1235 + cd submodule_update &&
1236 + git branch -t modify_sub1 origin/modify_sub1 &&
1237 + echo "gitdir: bogus/path" >sub1/.git &&
1238 + $command modify_sub1 &&
1239 + test_superproject_content origin/modify_sub1 &&
1240 + test_submodule_content sub1 origin/modify_sub1
1241 )
1242 '
1243 }