builtin / mv.con commit parse_pathspec: accept :(icase)path syntax (93d9353)
   1/*
   2 * "git mv" builtin command
   3 *
   4 * Copyright (C) 2006 Johannes Schindelin
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "dir.h"
   9#include "cache-tree.h"
  10#include "string-list.h"
  11#include "parse-options.h"
  12
  13static const char * const builtin_mv_usage[] = {
  14        N_("git mv [options] <source>... <destination>"),
  15        NULL
  16};
  17
  18static const char **internal_copy_pathspec(const char *prefix,
  19                                           const char **pathspec,
  20                                           int count, int base_name)
  21{
  22        int i;
  23        const char **result = xmalloc((count + 1) * sizeof(const char *));
  24        memcpy(result, pathspec, count * sizeof(const char *));
  25        result[count] = NULL;
  26        for (i = 0; i < count; i++) {
  27                int length = strlen(result[i]);
  28                int to_copy = length;
  29                while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
  30                        to_copy--;
  31                if (to_copy != length || base_name) {
  32                        char *it = xmemdupz(result[i], to_copy);
  33                        if (base_name) {
  34                                result[i] = xstrdup(basename(it));
  35                                free(it);
  36                        } else
  37                                result[i] = it;
  38                }
  39        }
  40        return get_pathspec(prefix, result);
  41}
  42
  43static const char *add_slash(const char *path)
  44{
  45        int len = strlen(path);
  46        if (path[len - 1] != '/') {
  47                char *with_slash = xmalloc(len + 2);
  48                memcpy(with_slash, path, len);
  49                with_slash[len++] = '/';
  50                with_slash[len] = 0;
  51                return with_slash;
  52        }
  53        return path;
  54}
  55
  56static struct lock_file lock_file;
  57
  58int cmd_mv(int argc, const char **argv, const char *prefix)
  59{
  60        int i, newfd;
  61        int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
  62        struct option builtin_mv_options[] = {
  63                OPT__VERBOSE(&verbose, N_("be verbose")),
  64                OPT__DRY_RUN(&show_only, N_("dry run")),
  65                OPT__FORCE(&force, N_("force move/rename even if target exists")),
  66                OPT_BOOLEAN('k', NULL, &ignore_errors, N_("skip move/rename errors")),
  67                OPT_END(),
  68        };
  69        const char **source, **destination, **dest_path;
  70        enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
  71        struct stat st;
  72        struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
  73
  74        git_config(git_default_config, NULL);
  75
  76        argc = parse_options(argc, argv, prefix, builtin_mv_options,
  77                             builtin_mv_usage, 0);
  78        if (--argc < 1)
  79                usage_with_options(builtin_mv_usage, builtin_mv_options);
  80
  81        newfd = hold_locked_index(&lock_file, 1);
  82        if (read_cache() < 0)
  83                die(_("index file corrupt"));
  84
  85        source = internal_copy_pathspec(prefix, argv, argc, 0);
  86        modes = xcalloc(argc, sizeof(enum update_mode));
  87        dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
  88
  89        if (dest_path[0][0] == '\0')
  90                /* special case: "." was normalized to "" */
  91                destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
  92        else if (!lstat(dest_path[0], &st) &&
  93                        S_ISDIR(st.st_mode)) {
  94                dest_path[0] = add_slash(dest_path[0]);
  95                destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
  96        } else {
  97                if (argc != 1)
  98                        die("destination '%s' is not a directory", dest_path[0]);
  99                destination = dest_path;
 100        }
 101
 102        /* Checking */
 103        for (i = 0; i < argc; i++) {
 104                const char *src = source[i], *dst = destination[i];
 105                int length, src_is_dir;
 106                const char *bad = NULL;
 107
 108                if (show_only)
 109                        printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
 110
 111                length = strlen(src);
 112                if (lstat(src, &st) < 0)
 113                        bad = _("bad source");
 114                else if (!strncmp(src, dst, length) &&
 115                                (dst[length] == 0 || dst[length] == '/')) {
 116                        bad = _("can not move directory into itself");
 117                } else if ((src_is_dir = S_ISDIR(st.st_mode))
 118                                && lstat(dst, &st) == 0)
 119                        bad = _("cannot move directory over file");
 120                else if (src_is_dir) {
 121                        const char *src_w_slash = add_slash(src);
 122                        int len_w_slash = length + 1;
 123                        int first, last;
 124
 125                        modes[i] = WORKING_DIRECTORY;
 126
 127                        first = cache_name_pos(src_w_slash, len_w_slash);
 128                        if (first >= 0)
 129                                die (_("Huh? %.*s is in index?"),
 130                                                len_w_slash, src_w_slash);
 131
 132                        first = -1 - first;
 133                        for (last = first; last < active_nr; last++) {
 134                                const char *path = active_cache[last]->name;
 135                                if (strncmp(path, src_w_slash, len_w_slash))
 136                                        break;
 137                        }
 138                        free((char *)src_w_slash);
 139
 140                        if (last - first < 1)
 141                                bad = _("source directory is empty");
 142                        else {
 143                                int j, dst_len;
 144
 145                                if (last - first > 0) {
 146                                        source = xrealloc(source,
 147                                                        (argc + last - first)
 148                                                        * sizeof(char *));
 149                                        destination = xrealloc(destination,
 150                                                        (argc + last - first)
 151                                                        * sizeof(char *));
 152                                        modes = xrealloc(modes,
 153                                                        (argc + last - first)
 154                                                        * sizeof(enum update_mode));
 155                                }
 156
 157                                dst = add_slash(dst);
 158                                dst_len = strlen(dst);
 159
 160                                for (j = 0; j < last - first; j++) {
 161                                        const char *path =
 162                                                active_cache[first + j]->name;
 163                                        source[argc + j] = path;
 164                                        destination[argc + j] =
 165                                                prefix_path(dst, dst_len,
 166                                                        path + length + 1);
 167                                        modes[argc + j] = INDEX;
 168                                }
 169                                argc += last - first;
 170                        }
 171                } else if (cache_name_pos(src, length) < 0)
 172                        bad = _("not under version control");
 173                else if (lstat(dst, &st) == 0) {
 174                        bad = _("destination exists");
 175                        if (force) {
 176                                /*
 177                                 * only files can overwrite each other:
 178                                 * check both source and destination
 179                                 */
 180                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
 181                                        if (verbose)
 182                                                warning(_("overwriting '%s'"), dst);
 183                                        bad = NULL;
 184                                } else
 185                                        bad = _("Cannot overwrite");
 186                        }
 187                } else if (string_list_has_string(&src_for_dst, dst))
 188                        bad = _("multiple sources for the same target");
 189                else
 190                        string_list_insert(&src_for_dst, dst);
 191
 192                if (bad) {
 193                        if (ignore_errors) {
 194                                if (--argc > 0) {
 195                                        memmove(source + i, source + i + 1,
 196                                                (argc - i) * sizeof(char *));
 197                                        memmove(destination + i,
 198                                                destination + i + 1,
 199                                                (argc - i) * sizeof(char *));
 200                                        i--;
 201                                }
 202                        } else
 203                                die (_("%s, source=%s, destination=%s"),
 204                                     bad, src, dst);
 205                }
 206        }
 207
 208        for (i = 0; i < argc; i++) {
 209                const char *src = source[i], *dst = destination[i];
 210                enum update_mode mode = modes[i];
 211                int pos;
 212                if (show_only || verbose)
 213                        printf(_("Renaming %s to %s\n"), src, dst);
 214                if (!show_only && mode != INDEX &&
 215                                rename(src, dst) < 0 && !ignore_errors)
 216                        die_errno (_("renaming '%s' failed"), src);
 217
 218                if (mode == WORKING_DIRECTORY)
 219                        continue;
 220
 221                pos = cache_name_pos(src, strlen(src));
 222                assert(pos >= 0);
 223                if (!show_only)
 224                        rename_cache_entry_at(pos, dst);
 225        }
 226
 227        if (active_cache_changed) {
 228                if (write_cache(newfd, active_cache, active_nr) ||
 229                    commit_locked_index(&lock_file))
 230                        die(_("Unable to write new index file"));
 231        }
 232
 233        return 0;
 234}