builtin / rm.con commit parse_pathspec: accept :(icase)path syntax (93d9353)
   1/*
   2 * "git rm" builtin command
   3 *
   4 * Copyright (C) Linus Torvalds 2006
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "dir.h"
   9#include "cache-tree.h"
  10#include "tree-walk.h"
  11#include "parse-options.h"
  12#include "string-list.h"
  13#include "submodule.h"
  14#include "pathspec.h"
  15
  16static const char * const builtin_rm_usage[] = {
  17        N_("git rm [options] [--] <file>..."),
  18        NULL
  19};
  20
  21static struct {
  22        int nr, alloc;
  23        struct {
  24                const char *name;
  25                char is_submodule;
  26        } *entry;
  27} list;
  28
  29static int get_ours_cache_pos(const char *path, int pos)
  30{
  31        int i = -pos - 1;
  32
  33        while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
  34                if (ce_stage(active_cache[i]) == 2)
  35                        return i;
  36                i++;
  37        }
  38        return -1;
  39}
  40
  41static void print_error_files(struct string_list *files_list,
  42                              const char *main_msg,
  43                              const char *hints_msg,
  44                              int *errs)
  45{
  46        if (files_list->nr) {
  47                int i;
  48                struct strbuf err_msg = STRBUF_INIT;
  49
  50                strbuf_addstr(&err_msg, main_msg);
  51                for (i = 0; i < files_list->nr; i++)
  52                        strbuf_addf(&err_msg,
  53                                    "\n    %s",
  54                                    files_list->items[i].string);
  55                if (advice_rm_hints)
  56                        strbuf_addstr(&err_msg, hints_msg);
  57                *errs = error("%s", err_msg.buf);
  58                strbuf_release(&err_msg);
  59        }
  60}
  61
  62static int check_submodules_use_gitfiles(void)
  63{
  64        int i;
  65        int errs = 0;
  66        struct string_list files = STRING_LIST_INIT_NODUP;
  67
  68        for (i = 0; i < list.nr; i++) {
  69                const char *name = list.entry[i].name;
  70                int pos;
  71                struct cache_entry *ce;
  72                struct stat st;
  73
  74                pos = cache_name_pos(name, strlen(name));
  75                if (pos < 0) {
  76                        pos = get_ours_cache_pos(name, pos);
  77                        if (pos < 0)
  78                                continue;
  79                }
  80                ce = active_cache[pos];
  81
  82                if (!S_ISGITLINK(ce->ce_mode) ||
  83                    (lstat(ce->name, &st) < 0) ||
  84                    is_empty_dir(name))
  85                        continue;
  86
  87                if (!submodule_uses_gitfile(name))
  88                        string_list_append(&files, name);
  89        }
  90        print_error_files(&files,
  91                          Q_("the following submodule (or one of its nested "
  92                             "submodules)\n uses a .git directory:",
  93                             "the following submodules (or one of its nested "
  94                             "submodules)\n use a .git directory:",
  95                             files.nr),
  96                          _("\n(use 'rm -rf' if you really want to remove "
  97                            "it including all of its history)"),
  98                          &errs);
  99        string_list_clear(&files, 0);
 100
 101        return errs;
 102}
 103
 104static int check_local_mod(unsigned char *head, int index_only)
 105{
 106        /*
 107         * Items in list are already sorted in the cache order,
 108         * so we could do this a lot more efficiently by using
 109         * tree_desc based traversal if we wanted to, but I am
 110         * lazy, and who cares if removal of files is a tad
 111         * slower than the theoretical maximum speed?
 112         */
 113        int i, no_head;
 114        int errs = 0;
 115        struct string_list files_staged = STRING_LIST_INIT_NODUP;
 116        struct string_list files_cached = STRING_LIST_INIT_NODUP;
 117        struct string_list files_submodule = STRING_LIST_INIT_NODUP;
 118        struct string_list files_local = STRING_LIST_INIT_NODUP;
 119
 120        no_head = is_null_sha1(head);
 121        for (i = 0; i < list.nr; i++) {
 122                struct stat st;
 123                int pos;
 124                struct cache_entry *ce;
 125                const char *name = list.entry[i].name;
 126                unsigned char sha1[20];
 127                unsigned mode;
 128                int local_changes = 0;
 129                int staged_changes = 0;
 130
 131                pos = cache_name_pos(name, strlen(name));
 132                if (pos < 0) {
 133                        /*
 134                         * Skip unmerged entries except for populated submodules
 135                         * that could lose history when removed.
 136                         */
 137                        pos = get_ours_cache_pos(name, pos);
 138                        if (pos < 0)
 139                                continue;
 140
 141                        if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
 142                            is_empty_dir(name))
 143                                continue;
 144                }
 145                ce = active_cache[pos];
 146
 147                if (lstat(ce->name, &st) < 0) {
 148                        if (errno != ENOENT && errno != ENOTDIR)
 149                                warning("'%s': %s", ce->name, strerror(errno));
 150                        /* It already vanished from the working tree */
 151                        continue;
 152                }
 153                else if (S_ISDIR(st.st_mode)) {
 154                        /* if a file was removed and it is now a
 155                         * directory, that is the same as ENOENT as
 156                         * far as git is concerned; we do not track
 157                         * directories unless they are submodules.
 158                         */
 159                        if (!S_ISGITLINK(ce->ce_mode))
 160                                continue;
 161                }
 162
 163                /*
 164                 * "rm" of a path that has changes need to be treated
 165                 * carefully not to allow losing local changes
 166                 * accidentally.  A local change could be (1) file in
 167                 * work tree is different since the index; and/or (2)
 168                 * the user staged a content that is different from
 169                 * the current commit in the index.
 170                 *
 171                 * In such a case, you would need to --force the
 172                 * removal.  However, "rm --cached" (remove only from
 173                 * the index) is safe if the index matches the file in
 174                 * the work tree or the HEAD commit, as it means that
 175                 * the content being removed is available elsewhere.
 176                 */
 177
 178                /*
 179                 * Is the index different from the file in the work tree?
 180                 * If it's a submodule, is its work tree modified?
 181                 */
 182                if (ce_match_stat(ce, &st, 0) ||
 183                    (S_ISGITLINK(ce->ce_mode) &&
 184                     !ok_to_remove_submodule(ce->name)))
 185                        local_changes = 1;
 186
 187                /*
 188                 * Is the index different from the HEAD commit?  By
 189                 * definition, before the very initial commit,
 190                 * anything staged in the index is treated by the same
 191                 * way as changed from the HEAD.
 192                 */
 193                if (no_head
 194                     || get_tree_entry(head, name, sha1, &mode)
 195                     || ce->ce_mode != create_ce_mode(mode)
 196                     || hashcmp(ce->sha1, sha1))
 197                        staged_changes = 1;
 198
 199                /*
 200                 * If the index does not match the file in the work
 201                 * tree and if it does not match the HEAD commit
 202                 * either, (1) "git rm" without --cached definitely
 203                 * will lose information; (2) "git rm --cached" will
 204                 * lose information unless it is about removing an
 205                 * "intent to add" entry.
 206                 */
 207                if (local_changes && staged_changes) {
 208                        if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD))
 209                                string_list_append(&files_staged, name);
 210                }
 211                else if (!index_only) {
 212                        if (staged_changes)
 213                                string_list_append(&files_cached, name);
 214                        if (local_changes) {
 215                                if (S_ISGITLINK(ce->ce_mode) &&
 216                                    !submodule_uses_gitfile(name))
 217                                        string_list_append(&files_submodule, name);
 218                                else
 219                                        string_list_append(&files_local, name);
 220                        }
 221                }
 222        }
 223        print_error_files(&files_staged,
 224                          Q_("the following file has staged content different "
 225                             "from both the\nfile and the HEAD:",
 226                             "the following files have staged content different"
 227                             " from both the\nfile and the HEAD:",
 228                             files_staged.nr),
 229                          _("\n(use -f to force removal)"),
 230                          &errs);
 231        string_list_clear(&files_staged, 0);
 232        print_error_files(&files_cached,
 233                          Q_("the following file has changes "
 234                             "staged in the index:",
 235                             "the following files have changes "
 236                             "staged in the index:", files_cached.nr),
 237                          _("\n(use --cached to keep the file,"
 238                            " or -f to force removal)"),
 239                          &errs);
 240        string_list_clear(&files_cached, 0);
 241        print_error_files(&files_submodule,
 242                          Q_("the following submodule (or one of its nested "
 243                             "submodule)\nuses a .git directory:",
 244                             "the following submodules (or one of its nested "
 245                             "submodule)\nuse a .git directory:",
 246                             files_submodule.nr),
 247                          _("\n(use 'rm -rf' if you really "
 248                            "want to remove it including all "
 249                            "of its history)"),
 250                          &errs);
 251        string_list_clear(&files_submodule, 0);
 252        print_error_files(&files_local,
 253                          Q_("the following file has local modifications:",
 254                             "the following files have local modifications:",
 255                             files_local.nr),
 256                          _("\n(use --cached to keep the file,"
 257                            " or -f to force removal)"),
 258                          &errs);
 259        string_list_clear(&files_local, 0);
 260
 261        return errs;
 262}
 263
 264static struct lock_file lock_file;
 265
 266static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
 267static int ignore_unmatch = 0;
 268
 269static struct option builtin_rm_options[] = {
 270        OPT__DRY_RUN(&show_only, N_("dry run")),
 271        OPT__QUIET(&quiet, N_("do not list removed files")),
 272        OPT_BOOLEAN( 0 , "cached",         &index_only, N_("only remove from the index")),
 273        OPT__FORCE(&force, N_("override the up-to-date check")),
 274        OPT_BOOLEAN('r', NULL,             &recursive,  N_("allow recursive removal")),
 275        OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch,
 276                                N_("exit with a zero status even if nothing matched")),
 277        OPT_END(),
 278};
 279
 280int cmd_rm(int argc, const char **argv, const char *prefix)
 281{
 282        int i, newfd;
 283        struct pathspec pathspec;
 284        char *seen;
 285
 286        git_config(git_default_config, NULL);
 287
 288        argc = parse_options(argc, argv, prefix, builtin_rm_options,
 289                             builtin_rm_usage, 0);
 290        if (!argc)
 291                usage_with_options(builtin_rm_usage, builtin_rm_options);
 292
 293        if (!index_only)
 294                setup_work_tree();
 295
 296        newfd = hold_locked_index(&lock_file, 1);
 297
 298        if (read_cache() < 0)
 299                die(_("index file corrupt"));
 300
 301        /*
 302         * Drop trailing directory separators from directories so we'll find
 303         * submodules in the index.
 304         */
 305        for (i = 0; i < argc; i++) {
 306                size_t pathlen = strlen(argv[i]);
 307                if (pathlen && is_dir_sep(argv[i][pathlen - 1]) &&
 308                    is_directory(argv[i])) {
 309                        do {
 310                                pathlen--;
 311                        } while (pathlen && is_dir_sep(argv[i][pathlen - 1]));
 312                        argv[i] = xmemdupz(argv[i], pathlen);
 313                }
 314        }
 315
 316        parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD, prefix, argv);
 317        refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);
 318
 319        seen = NULL;
 320        seen = xcalloc(pathspec.nr, 1);
 321
 322        for (i = 0; i < active_nr; i++) {
 323                struct cache_entry *ce = active_cache[i];
 324                if (!match_pathspec_depth(&pathspec, ce->name, ce_namelen(ce), 0, seen))
 325                        continue;
 326                ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
 327                list.entry[list.nr].name = ce->name;
 328                list.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);
 329        }
 330
 331        if (pathspec.nr) {
 332                const char *original;
 333                int seen_any = 0;
 334                for (i = 0; i < pathspec.nr; i++) {
 335                        original = pathspec.items[i].original;
 336                        if (!seen[i]) {
 337                                if (!ignore_unmatch) {
 338                                        die(_("pathspec '%s' did not match any files"),
 339                                            original);
 340                                }
 341                        }
 342                        else {
 343                                seen_any = 1;
 344                        }
 345                        if (!recursive && seen[i] == MATCHED_RECURSIVELY)
 346                                die(_("not removing '%s' recursively without -r"),
 347                                    *original ? original : ".");
 348                }
 349
 350                if (! seen_any)
 351                        exit(0);
 352        }
 353
 354        /*
 355         * If not forced, the file, the index and the HEAD (if exists)
 356         * must match; but the file can already been removed, since
 357         * this sequence is a natural "novice" way:
 358         *
 359         *      rm F; git rm F
 360         *
 361         * Further, if HEAD commit exists, "diff-index --cached" must
 362         * report no changes unless forced.
 363         */
 364        if (!force) {
 365                unsigned char sha1[20];
 366                if (get_sha1("HEAD", sha1))
 367                        hashclr(sha1);
 368                if (check_local_mod(sha1, index_only))
 369                        exit(1);
 370        } else if (!index_only) {
 371                if (check_submodules_use_gitfiles())
 372                        exit(1);
 373        }
 374
 375        /*
 376         * First remove the names from the index: we won't commit
 377         * the index unless all of them succeed.
 378         */
 379        for (i = 0; i < list.nr; i++) {
 380                const char *path = list.entry[i].name;
 381                if (!quiet)
 382                        printf("rm '%s'\n", path);
 383
 384                if (remove_file_from_cache(path))
 385                        die(_("git rm: unable to remove %s"), path);
 386        }
 387
 388        if (show_only)
 389                return 0;
 390
 391        /*
 392         * Then, unless we used "--cached", remove the filenames from
 393         * the workspace. If we fail to remove the first one, we
 394         * abort the "git rm" (but once we've successfully removed
 395         * any file at all, we'll go ahead and commit to it all:
 396         * by then we've already committed ourselves and can't fail
 397         * in the middle)
 398         */
 399        if (!index_only) {
 400                int removed = 0;
 401                for (i = 0; i < list.nr; i++) {
 402                        const char *path = list.entry[i].name;
 403                        if (list.entry[i].is_submodule) {
 404                                if (is_empty_dir(path)) {
 405                                        if (!rmdir(path)) {
 406                                                removed = 1;
 407                                                continue;
 408                                        }
 409                                } else {
 410                                        struct strbuf buf = STRBUF_INIT;
 411                                        strbuf_addstr(&buf, path);
 412                                        if (!remove_dir_recursively(&buf, 0)) {
 413                                                removed = 1;
 414                                                strbuf_release(&buf);
 415                                                continue;
 416                                        }
 417                                        strbuf_release(&buf);
 418                                        /* Fallthrough and let remove_path() fail. */
 419                                }
 420                        }
 421                        if (!remove_path(path)) {
 422                                removed = 1;
 423                                continue;
 424                        }
 425                        if (!removed)
 426                                die_errno("git rm: '%s'", path);
 427                }
 428        }
 429
 430        if (active_cache_changed) {
 431                if (write_cache(newfd, active_cache, active_nr) ||
 432                    commit_locked_index(&lock_file))
 433                        die(_("Unable to write new index file"));
 434        }
 435
 436        return 0;
 437}