1437
const char *path,
1438
unsigned flags)
1439
{
1440
- const char *sub_git_dir, *v;
1441
- char *real_sub_git_dir = NULL, *real_common_git_dir = NULL;
1440
+ int err_code;
1441
+ const char *sub_git_dir;
1442
struct strbuf gitdir = STRBUF_INIT;
1443
-
1443
strbuf_addf(&gitdir, "%s/.git", path);
1445
- sub_git_dir = resolve_gitdir(gitdir.buf);
1444
+ sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
1445
1446
/* Not populated? */
1448
- if (!sub_git_dir)
1449
- goto out;
1447
+ if (!sub_git_dir) {
1448
+ char *real_new_git_dir;
1449
+ const char *new_git_dir;
1450
+ const struct submodule *sub;
1451
+
1452
+ if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1453
+ /* unpopulated as expected */
1454
+ strbuf_release(&gitdir);
1455
+ return;
1456
+ }
1457
+
1458
+ if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1459
+ /* We don't know what broke here. */
1460
+ read_gitfile_error_die(err_code, path, NULL);
1461
+
1462
+ /*
1463
+ * Maybe populated, but no git directory was found?
1464
+ * This can happen if the superproject is a submodule
1465
+ * itself and was just absorbed. The absorption of the
1466
+ * superproject did not rewrite the git file links yet,
1467
+ * fix it now.
1468
+ */
1469
+ sub = submodule_from_path(null_sha1, path);
1470
+ if (!sub)
1471
+ die(_("could not lookup name for submodule '%s'"), path);
1472
+ new_git_dir = git_path("modules/%s", sub->name);
1473
+ if (safe_create_leading_directories_const(new_git_dir) < 0)
1474
+ die(_("could not create directory '%s'"), new_git_dir);
1475
+ real_new_git_dir = real_pathdup(new_git_dir);
1476
+ connect_work_tree_and_git_dir(path, real_new_git_dir);
1477
+
1478
+ free(real_new_git_dir);
1479
+ } else {
1480
+ /* Is it already absorbed into the superprojects git dir? */
1481
+ char *real_sub_git_dir = real_pathdup(sub_git_dir);
1482
+ char *real_common_git_dir = real_pathdup(get_git_common_dir());
1483
1451
- /* Is it already absorbed into the superprojects git dir? */
1452
- real_sub_git_dir = real_pathdup(sub_git_dir);
1453
- real_common_git_dir = real_pathdup(get_git_common_dir());
1454
- if (!skip_prefix(real_sub_git_dir, real_common_git_dir, &v))
1455
- relocate_single_git_dir_into_superproject(prefix, path);
1484
+ if (!starts_with(real_sub_git_dir, real_common_git_dir))
1485
+ relocate_single_git_dir_into_superproject(prefix, path);
1486
+
1487
+ free(real_sub_git_dir);
1488
+ free(real_common_git_dir);
1489
+ }
1490
+ strbuf_release(&gitdir);
1491
1492
if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1493
struct child_process cp = CHILD_PROCESS_INIT;
1513
1514
strbuf_release(&sb);
1515
}
1481
-
1482
-out:
1483
- strbuf_release(&gitdir);
1484
- free(real_sub_git_dir);
1485
- free(real_common_git_dir);
1516
}