1641
return sm_gitdir;
1642
}
1643
1644
+static int dir_contains_only_dotgit(const char *path)
1645
+{
1646
+ DIR *dir = opendir(path);
1647
+ struct dirent *e;
1648
+ int ret = 1;
1649
+
1650
+ if (!dir)
1651
+ return 0;
1652
+
1653
+ e = readdir_skip_dot_and_dotdot(dir);
1654
+ if (!e)
1655
+ ret = 0;
1656
+ else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT, e->d_name) ||
1657
+ (e = readdir_skip_dot_and_dotdot(dir))) {
1658
+ error("unexpected item '%s' in '%s'", e->d_name, path);
1659
+ ret = 0;
1660
+ }
1661
+
1662
+ closedir(dir);
1663
+ return ret;
1664
+}
1665
+
1666
static int clone_submodule(const struct module_clone_data *clone_data,
1667
struct string_list *reference)
1668
{
1669
char *p;
1670
char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1671
char *sm_alternate = NULL, *error_strategy = NULL;
1672
+ struct stat st;
1673
struct child_process cp = CHILD_PROCESS_INIT;
1674
const char *clone_data_path = clone_data->path;
1675
char *to_free = NULL;
1683
"git dir"), sm_gitdir);
1684
1685
if (!file_exists(sm_gitdir)) {
1686
+ if (clone_data->require_init && !stat(clone_data_path, &st) &&
1687
+ !is_empty_dir(clone_data_path))
1688
+ die(_("directory not empty: '%s'"), clone_data_path);
1689
+
1690
if (safe_create_leading_directories_const(sm_gitdir) < 0)
1691
die(_("could not create directory '%s'"), sm_gitdir);
1692
1731
if(run_command(&cp))
1732
die(_("clone of '%s' into submodule path '%s' failed"),
1733
clone_data->url, clone_data_path);
1734
+
1735
+ if (clone_data->require_init && !stat(clone_data_path, &st) &&
1736
+ !dir_contains_only_dotgit(clone_data_path)) {
1737
+ char *dot_git = xstrfmt("%s/.git", clone_data_path);
1738
+ unlink(dot_git);
1739
+ free(dot_git);
1740
+ die(_("directory not empty: '%s'"), clone_data_path);
1741
+ }
1742
} else {
1743
char *path;
1744