submodule: rename and add flags to ok_to_remove_submodule

In different contexts the question "Is it ok to delete a submodule?" may be answered differently. In 293ab15eea (submodule: teach rm to remove submodules unless they contain a git directory, 2012-09-26) a case was made that we can safely ignore ignored untracked files for removal as we explicitely ask for the removal of the submodule. In a later patch we want to remove submodules even when the user doesn't explicitly ask for it (e.g. checking out a tree-ish in which the submodule doesn't exist). In that case we want to be more careful when it comes to deletion of untracked files. As of this patch it is unclear how this will be implemented exactly, so we'll offer flags in which the caller can specify how the different untracked files ought to be handled. As the flags allow the function to not die on an error when spawning a child process, we need to find an appropriate return code for the case when the child process could not be started. As in that case we cannot tell if the submodule is ok to remove, we'd want to return 'false'. As only 0 is understood as false, rename the function to invert the meaning, i.e. the return code of 0 signals the removal of the submodule is fine, and other values can be used to return a more precise answer what went wrong. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Dec 20, 2016 at 15:20 UTC 83b7696605dbea3eb4e878cf904159f398345aa9
3 files changed +45 -14
builtin/rm.c
+3 -1
@@ -187,7 +187,9 @@ static int check_local_mod(struct object_id *head, int index_only)
187 */
188 if (ce_match_stat(ce, &st, 0) ||
189 (S_ISGITLINK(ce->ce_mode) &&
190 - !ok_to_remove_submodule(ce->name)))
190 + bad_to_remove_submodule(ce->name,
191 + SUBMODULE_REMOVAL_DIE_ON_ERROR |
192 + SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
193 local_changes = 1;
194
195 /*
submodule.c
+37 -12
@@ -1019,39 +1019,64 @@ int submodule_uses_gitfile(const char *path)
1019 return 1;
1020 }
1021
1022 -int ok_to_remove_submodule(const char *path)
1022 +/*
1023 + * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1024 + * when doing so.
1025 + *
1026 + * Return 1 if we'd lose data, return 0 if the removal is fine,
1027 + * and negative values for errors.
1028 + */
1029 +int bad_to_remove_submodule(const char *path, unsigned flags)
1030 {
1031 ssize_t len;
1032 struct child_process cp = CHILD_PROCESS_INIT;
1033 struct strbuf buf = STRBUF_INIT;
1027 - int ok_to_remove = 1;
1034 + int ret = 0;
1035
1036 if (!file_exists(path) || is_empty_dir(path))
1030 - return 1;
1037 + return 0;
1038
1039 if (!submodule_uses_gitfile(path))
1033 - return 0;
1040 + return 1;
1041
1035 - argv_array_pushl(&cp.args, "status", "--porcelain", "-u",
1042 + argv_array_pushl(&cp.args, "status", "--porcelain",
1043 "--ignore-submodules=none", NULL);
1044 +
1045 + if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1046 + argv_array_push(&cp.args, "-uno");
1047 + else
1048 + argv_array_push(&cp.args, "-uall");
1049 +
1050 + if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1051 + argv_array_push(&cp.args, "--ignored");
1052 +
1053 prepare_submodule_repo_env(&cp.env_array);
1054 cp.git_cmd = 1;
1055 cp.no_stdin = 1;
1056 cp.out = -1;
1057 cp.dir = path;
1042 - if (start_command(&cp))
1043 - die(_("could not run 'git status --porcelain -u --ignore-submodules=none' in submodule %s"), path);
1058 + if (start_command(&cp)) {
1059 + if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1060 + die(_("could not start 'git status in submodule '%s'"),
1061 + path);
1062 + ret = -1;
1063 + goto out;
1064 + }
1065
1066 len = strbuf_read(&buf, cp.out, 1024);
1067 if (len > 2)
1047 - ok_to_remove = 0;
1068 + ret = 1;
1069 close(cp.out);
1070
1050 - if (finish_command(&cp))
1051 - die(_("'git status --porcelain -u --ignore-submodules=none' failed in submodule %s"), path);
1052 -
1071 + if (finish_command(&cp)) {
1072 + if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1073 + die(_("could not run 'git status in submodule '%s'"),
1074 + path);
1075 + ret = -1;
1076 + }
1077 +out:
1078 strbuf_release(&buf);
1054 - return ok_to_remove;
1079 + return ret;
1080 }
1081
1082 static int find_first_merges(struct object_array *result, const char *path,
submodule.h
+5 -1
@@ -59,7 +59,11 @@ extern int fetch_populated_submodules(const struct argv_array *options,
59 int quiet, int max_parallel_jobs);
60 extern unsigned is_submodule_modified(const char *path, int ignore_untracked);
61 extern int submodule_uses_gitfile(const char *path);
62 -extern int ok_to_remove_submodule(const char *path);
62 +
63 +#define SUBMODULE_REMOVAL_DIE_ON_ERROR (1<<0)
64 +#define SUBMODULE_REMOVAL_IGNORE_UNTRACKED (1<<1)
65 +#define SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED (1<<2)
66 +extern int bad_to_remove_submodule(const char *path, unsigned flags);
67 extern int merge_submodule(unsigned char result[20], const char *path,
68 const unsigned char base[20],
69 const unsigned char a[20],