]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/rm.c
Sync with 2.31.5
[thirdparty/git.git] / builtin / rm.c
index 5559a0b453a3566d85f6a550332ecd9ee8ebea88..8a24c715e02bab24098af5f3e354c631ee9abf3c 100644 (file)
@@ -5,6 +5,7 @@
  */
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
+#include "advice.h"
 #include "config.h"
 #include "lockfile.h"
 #include "dir.h"
@@ -254,7 +255,7 @@ static struct option builtin_rm_options[] = {
 int cmd_rm(int argc, const char **argv, const char *prefix)
 {
        struct lock_file lock_file = LOCK_INIT;
-       int i;
+       int i, ret = 0;
        struct pathspec pathspec;
        char *seen;
 
@@ -297,6 +298,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
        ensure_full_index(&the_index);
        for (i = 0; i < active_nr; i++) {
                const struct cache_entry *ce = active_cache[i];
+               if (ce_skip_worktree(ce))
+                       continue;
                if (!ce_path_match(&the_index, ce, &pathspec, seen))
                        continue;
                ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
@@ -310,25 +313,37 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
        if (pathspec.nr) {
                const char *original;
                int seen_any = 0;
+               char *skip_worktree_seen = NULL;
+               struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
+
                for (i = 0; i < pathspec.nr; i++) {
                        original = pathspec.items[i].original;
-                       if (!seen[i]) {
-                               if (!ignore_unmatch) {
-                                       die(_("pathspec '%s' did not match any files"),
-                                           original);
-                               }
-                       }
-                       else {
+                       if (seen[i])
                                seen_any = 1;
-                       }
+                       else if (ignore_unmatch)
+                               continue;
+                       else if (matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
+                               string_list_append(&only_match_skip_worktree, original);
+                       else
+                               die(_("pathspec '%s' did not match any files"), original);
+
                        if (!recursive && seen[i] == MATCHED_RECURSIVELY)
                                die(_("not removing '%s' recursively without -r"),
                                    *original ? original : ".");
                }
 
+               if (only_match_skip_worktree.nr) {
+                       advise_on_updating_sparse_paths(&only_match_skip_worktree);
+                       ret = 1;
+               }
+               free(skip_worktree_seen);
+               string_list_clear(&only_match_skip_worktree, 0);
+
                if (!seen_any)
-                       exit(0);
+                       exit(ret);
        }
+       clear_pathspec(&pathspec);
+       free(seen);
 
        if (!index_only)
                submodules_absorb_gitdir_if_needed();
@@ -407,5 +422,5 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
                               COMMIT_LOCK | SKIP_IF_UNCHANGED))
                die(_("Unable to write new index file"));
 
-       return 0;
+       return ret;
 }