1842
return parallel_jobs;
1843
}
1844
1845
+int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
1846
+{
1847
+ size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
1848
+ char *p;
1849
+ int ret = 0;
1850
+
1851
+ if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
1852
+ strcmp(p, submodule_name))
1853
+ BUG("submodule name '%s' not a suffix of git dir '%s'",
1854
+ submodule_name, git_dir);
1855
+
1856
+ /*
1857
+ * We prevent the contents of sibling submodules' git directories to
1858
+ * clash.
1859
+ *
1860
+ * Example: having a submodule named `hippo` and another one named
1861
+ * `hippo/hooks` would result in the git directories
1862
+ * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
1863
+ * but the latter directory is already designated to contain the hooks
1864
+ * of the former.
1865
+ */
1866
+ for (; *p; p++) {
1867
+ if (is_dir_sep(*p)) {
1868
+ char c = *p;
1869
+
1870
+ *p = '\0';
1871
+ if (is_git_directory(git_dir))
1872
+ ret = -1;
1873
+ *p = c;
1874
+
1875
+ if (ret < 0)
1876
+ return error(_("submodule git dir '%s' is "
1877
+ "inside git dir '%.*s'"),
1878
+ git_dir,
1879
+ (int)(p - git_dir), git_dir);
1880
+ }
1881
+ }
1882
+
1883
+ return 0;
1884
+}
1885
+
1886
/*
1887
* Embeds a single submodules git directory into the superprojects git dir,
1888
* non recursively.
1891
const char *path)
1892
{
1893
char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1853
- const char *new_git_dir;
1894
+ char *new_git_dir;
1895
const struct submodule *sub;
1896
1897
if (submodule_uses_worktrees(path))
1909
if (!sub)
1910
die(_("could not lookup name for submodule '%s'"), path);
1911
1871
- new_git_dir = git_path("modules/%s", sub->name);
1912
+ new_git_dir = git_pathdup("modules/%s", sub->name);
1913
+ if (validate_submodule_git_dir(new_git_dir, sub->name) < 0)
1914
+ die(_("refusing to move '%s' into an existing git dir"),
1915
+ real_old_git_dir);
1916
if (safe_create_leading_directories_const(new_git_dir) < 0)
1917
die(_("could not create directory '%s'"), new_git_dir);
1918
real_new_git_dir = real_pathdup(new_git_dir, 1);
1919
+ free(new_git_dir);
1920
1921
fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1922
get_super_prefix_or_empty(), path,