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