builtin-rm.con commit lock_ref_sha1_basic does not remove empty directories on BSD (cb626bc)
   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
  11static const char builtin_rm_usage[] =
  12"git-rm [-n] [-v] [-f] <filepattern>...";
  13
  14static struct {
  15        int nr, alloc;
  16        const char **name;
  17} list;
  18
  19static void add_list(const char *name)
  20{
  21        if (list.nr >= list.alloc) {
  22                list.alloc = alloc_nr(list.alloc);
  23                list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
  24        }
  25        list.name[list.nr++] = name;
  26}
  27
  28static int remove_file(const char *name)
  29{
  30        int ret;
  31        char *slash;
  32
  33        ret = unlink(name);
  34        if (!ret && (slash = strrchr(name, '/'))) {
  35                char *n = xstrdup(name);
  36                do {
  37                        n[slash - name] = 0;
  38                        name = n;
  39                } while (!rmdir(name) && (slash = strrchr(name, '/')));
  40        }
  41        return ret;
  42}
  43
  44static struct lock_file lock_file;
  45
  46int cmd_rm(int argc, const char **argv, const char *prefix)
  47{
  48        int i, newfd;
  49        int verbose = 0, show_only = 0, force = 0;
  50        const char **pathspec;
  51        char *seen;
  52
  53        git_config(git_default_config);
  54
  55        newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
  56
  57        if (read_cache() < 0)
  58                die("index file corrupt");
  59
  60        for (i = 1 ; i < argc ; i++) {
  61                const char *arg = argv[i];
  62
  63                if (*arg != '-')
  64                        break;
  65                if (!strcmp(arg, "--")) {
  66                        i++;
  67                        break;
  68                }
  69                if (!strcmp(arg, "-n")) {
  70                        show_only = 1;
  71                        continue;
  72                }
  73                if (!strcmp(arg, "-v")) {
  74                        verbose = 1;
  75                        continue;
  76                }
  77                if (!strcmp(arg, "-f")) {
  78                        force = 1;
  79                        continue;
  80                }
  81                usage(builtin_rm_usage);
  82        }
  83        if (argc <= i)
  84                usage(builtin_rm_usage);
  85
  86        pathspec = get_pathspec(prefix, argv + i);
  87        seen = NULL;
  88        for (i = 0; pathspec[i] ; i++)
  89                /* nothing */;
  90        seen = xcalloc(i, 1);
  91
  92        for (i = 0; i < active_nr; i++) {
  93                struct cache_entry *ce = active_cache[i];
  94                if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
  95                        continue;
  96                add_list(ce->name);
  97        }
  98
  99        if (pathspec) {
 100                const char *match;
 101                for (i = 0; (match = pathspec[i]) != NULL ; i++) {
 102                        if (*match && !seen[i])
 103                                die("pathspec '%s' did not match any files", match);
 104                }
 105        }
 106
 107        /*
 108         * First remove the names from the index: we won't commit
 109         * the index unless all of them succeed
 110         */
 111        for (i = 0; i < list.nr; i++) {
 112                const char *path = list.name[i];
 113                printf("rm '%s'\n", path);
 114
 115                if (remove_file_from_cache(path))
 116                        die("git-rm: unable to remove %s", path);
 117                cache_tree_invalidate_path(active_cache_tree, path);
 118        }
 119
 120        if (show_only)
 121                return 0;
 122
 123        /*
 124         * Then, if we used "-f", remove the filenames from the
 125         * workspace. If we fail to remove the first one, we
 126         * abort the "git rm" (but once we've successfully removed
 127         * any file at all, we'll go ahead and commit to it all:
 128         * by then we've already committed ourselves and can't fail
 129         * in the middle)
 130         */
 131        if (force) {
 132                int removed = 0;
 133                for (i = 0; i < list.nr; i++) {
 134                        const char *path = list.name[i];
 135                        if (!remove_file(path)) {
 136                                removed = 1;
 137                                continue;
 138                        }
 139                        if (!removed)
 140                                die("git-rm: %s: %s", path, strerror(errno));
 141                }
 142        }
 143
 144        if (active_cache_changed) {
 145                if (write_cache(newfd, active_cache, active_nr) ||
 146                    close(newfd) || commit_lock_file(&lock_file))
 147                        die("Unable to write new index file");
 148        }
 149
 150        return 0;
 151}