Merge branch 'aj/p4-symlink-lose-nl'
[gitweb.git] / builtin / rm.c
index 06025a2e751fbc2b2de3b024919c381cd2176c51..d00eaf86d56c9efbd436458d46937b8b2c855ce4 100644 (file)
@@ -58,6 +58,21 @@ static void print_error_files(struct string_list *files_list,
        }
 }
 
+static void error_removing_concrete_submodules(struct string_list *files, int *errs)
+{
+       print_error_files(files,
+                         Q_("the following submodule (or one of its nested "
+                            "submodules)\n"
+                            "uses a .git directory:",
+                            "the following submodules (or one of its nested "
+                            "submodules)\n"
+                            "use a .git directory:", files->nr),
+                         _("\n(use 'rm -rf' if you really want to remove "
+                           "it including all of its history)"),
+                         errs);
+       string_list_clear(files, 0);
+}
+
 static int check_submodules_use_gitfiles(void)
 {
        int i;
@@ -67,7 +82,7 @@ static int check_submodules_use_gitfiles(void)
        for (i = 0; i < list.nr; i++) {
                const char *name = list.entry[i].name;
                int pos;
-               struct cache_entry *ce;
+               const struct cache_entry *ce;
                struct stat st;
 
                pos = cache_name_pos(name, strlen(name));
@@ -86,16 +101,8 @@ static int check_submodules_use_gitfiles(void)
                if (!submodule_uses_gitfile(name))
                        string_list_append(&files, name);
        }
-       print_error_files(&files,
-                         Q_("the following submodule (or one of its nested "
-                            "submodules)\n uses a .git directory:",
-                            "the following submodules (or one of its nested "
-                            "submodules)\n use a .git directory:",
-                            files.nr),
-                         _("\n(use 'rm -rf' if you really want to remove "
-                           "it including all of its history)"),
-                         &errs);
-       string_list_clear(&files, 0);
+
+       error_removing_concrete_submodules(&files, &errs);
 
        return errs;
 }
@@ -120,7 +127,7 @@ static int check_local_mod(unsigned char *head, int index_only)
        for (i = 0; i < list.nr; i++) {
                struct stat st;
                int pos;
-               struct cache_entry *ce;
+               const struct cache_entry *ce;
                const char *name = list.entry[i].name;
                unsigned char sha1[20];
                unsigned mode;
@@ -237,17 +244,9 @@ static int check_local_mod(unsigned char *head, int index_only)
                            " or -f to force removal)"),
                          &errs);
        string_list_clear(&files_cached, 0);
-       print_error_files(&files_submodule,
-                         Q_("the following submodule (or one of its nested "
-                            "submodule)\nuses a .git directory:",
-                            "the following submodules (or one of its nested "
-                            "submodule)\nuse a .git directory:",
-                            files_submodule.nr),
-                         _("\n(use 'rm -rf' if you really "
-                           "want to remove it including all "
-                           "of its history)"),
-                         &errs);
-       string_list_clear(&files_submodule, 0);
+
+       error_removing_concrete_submodules(&files_submodule, &errs);
+
        print_error_files(&files_local,
                          Q_("the following file has local modifications:",
                             "the following files have local modifications:",
@@ -278,8 +277,8 @@ static struct option builtin_rm_options[] = {
 
 int cmd_rm(int argc, const char **argv, const char *prefix)
 {
-       int i, newfd;
-       const char **pathspec;
+       int i, newfd, seen_any;
+       const char **pathspec, *match;
        char *seen;
 
        git_config(git_default_config, NULL);
@@ -315,13 +314,12 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
        pathspec = get_pathspec(prefix, argv);
        refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
 
-       seen = NULL;
        for (i = 0; pathspec[i] ; i++)
                /* nothing */;
        seen = xcalloc(i, 1);
 
        for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
+               const struct cache_entry *ce = active_cache[i];
                if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
                        continue;
                ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
@@ -329,27 +327,24 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
                list.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);
        }
 
-       if (pathspec) {
-               const char *match;
-               int seen_any = 0;
-               for (i = 0; (match = pathspec[i]) != NULL ; i++) {
-                       if (!seen[i]) {
-                               if (!ignore_unmatch) {
-                                       die(_("pathspec '%s' did not match any files"),
-                                           match);
-                               }
-                       }
-                       else {
-                               seen_any = 1;
+
+       seen_any = 0;
+       for (i = 0; (match = pathspec[i]) != NULL ; i++) {
+               if (!seen[i]) {
+                       if (!ignore_unmatch) {
+                               die(_("pathspec '%s' did not match any files"),
+                                   match);
                        }
-                       if (!recursive && seen[i] == MATCHED_RECURSIVELY)
-                               die(_("not removing '%s' recursively without -r"),
-                                   *match ? match : ".");
                }
-
-               if (! seen_any)
-                       exit(0);
+               else {
+                       seen_any = 1;
+               }
+               if (!recursive && seen[i] == MATCHED_RECURSIVELY)
+                       die(_("not removing '%s' recursively without -r"),
+                           *match ? match : ".");
        }
+       if (!seen_any)
+               exit(0);
 
        /*
         * If not forced, the file, the index and the HEAD (if exists)