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