builtin / mv.con commit merge: add scissors line on merge conflict (1055997)
   1/*
   2 * "git mv" builtin command
   3 *
   4 * Copyright (C) 2006 Johannes Schindelin
   5 */
   6#define USE_THE_INDEX_COMPATIBILITY_MACROS
   7#include "builtin.h"
   8#include "config.h"
   9#include "pathspec.h"
  10#include "lockfile.h"
  11#include "dir.h"
  12#include "cache-tree.h"
  13#include "string-list.h"
  14#include "parse-options.h"
  15#include "submodule.h"
  16
  17static const char * const builtin_mv_usage[] = {
  18        N_("git mv [<options>] <source>... <destination>"),
  19        NULL
  20};
  21
  22#define DUP_BASENAME 1
  23#define KEEP_TRAILING_SLASH 2
  24
  25static const char **internal_prefix_pathspec(const char *prefix,
  26                                             const char **pathspec,
  27                                             int count, unsigned flags)
  28{
  29        int i;
  30        const char **result;
  31        int prefixlen = prefix ? strlen(prefix) : 0;
  32        ALLOC_ARRAY(result, count + 1);
  33
  34        /* Create an intermediate copy of the pathspec based on the flags */
  35        for (i = 0; i < count; i++) {
  36                int length = strlen(pathspec[i]);
  37                int to_copy = length;
  38                char *it;
  39                while (!(flags & KEEP_TRAILING_SLASH) &&
  40                       to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
  41                        to_copy--;
  42
  43                it = xmemdupz(pathspec[i], to_copy);
  44                if (flags & DUP_BASENAME) {
  45                        result[i] = xstrdup(basename(it));
  46                        free(it);
  47                } else {
  48                        result[i] = it;
  49                }
  50        }
  51        result[count] = NULL;
  52
  53        /* Prefix the pathspec and free the old intermediate strings */
  54        for (i = 0; i < count; i++) {
  55                const char *match = prefix_path(prefix, prefixlen, result[i]);
  56                free((char *) result[i]);
  57                result[i] = match;
  58        }
  59
  60        return result;
  61}
  62
  63static const char *add_slash(const char *path)
  64{
  65        size_t len = strlen(path);
  66        if (path[len - 1] != '/') {
  67                char *with_slash = xmalloc(st_add(len, 2));
  68                memcpy(with_slash, path, len);
  69                with_slash[len++] = '/';
  70                with_slash[len] = 0;
  71                return with_slash;
  72        }
  73        return path;
  74}
  75
  76#define SUBMODULE_WITH_GITDIR ((const char *)1)
  77
  78static void prepare_move_submodule(const char *src, int first,
  79                                   const char **submodule_gitfile)
  80{
  81        struct strbuf submodule_dotgit = STRBUF_INIT;
  82        if (!S_ISGITLINK(active_cache[first]->ce_mode))
  83                die(_("Directory %s is in index and no submodule?"), src);
  84        if (!is_staging_gitmodules_ok(&the_index))
  85                die(_("Please stage your changes to .gitmodules or stash them to proceed"));
  86        strbuf_addf(&submodule_dotgit, "%s/.git", src);
  87        *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
  88        if (*submodule_gitfile)
  89                *submodule_gitfile = xstrdup(*submodule_gitfile);
  90        else
  91                *submodule_gitfile = SUBMODULE_WITH_GITDIR;
  92        strbuf_release(&submodule_dotgit);
  93}
  94
  95static int index_range_of_same_dir(const char *src, int length,
  96                                   int *first_p, int *last_p)
  97{
  98        const char *src_w_slash = add_slash(src);
  99        int first, last, len_w_slash = length + 1;
 100
 101        first = cache_name_pos(src_w_slash, len_w_slash);
 102        if (first >= 0)
 103                die(_("%.*s is in index"), len_w_slash, src_w_slash);
 104
 105        first = -1 - first;
 106        for (last = first; last < active_nr; last++) {
 107                const char *path = active_cache[last]->name;
 108                if (strncmp(path, src_w_slash, len_w_slash))
 109                        break;
 110        }
 111        if (src_w_slash != src)
 112                free((char *)src_w_slash);
 113        *first_p = first;
 114        *last_p = last;
 115        return last - first;
 116}
 117
 118int cmd_mv(int argc, const char **argv, const char *prefix)
 119{
 120        int i, flags, gitmodules_modified = 0;
 121        int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
 122        struct option builtin_mv_options[] = {
 123                OPT__VERBOSE(&verbose, N_("be verbose")),
 124                OPT__DRY_RUN(&show_only, N_("dry run")),
 125                OPT__FORCE(&force, N_("force move/rename even if target exists"),
 126                           PARSE_OPT_NOCOMPLETE),
 127                OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
 128                OPT_END(),
 129        };
 130        const char **source, **destination, **dest_path, **submodule_gitfile;
 131        enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
 132        struct stat st;
 133        struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 134        struct lock_file lock_file = LOCK_INIT;
 135
 136        git_config(git_default_config, NULL);
 137
 138        argc = parse_options(argc, argv, prefix, builtin_mv_options,
 139                             builtin_mv_usage, 0);
 140        if (--argc < 1)
 141                usage_with_options(builtin_mv_usage, builtin_mv_options);
 142
 143        hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 144        if (read_cache() < 0)
 145                die(_("index file corrupt"));
 146
 147        source = internal_prefix_pathspec(prefix, argv, argc, 0);
 148        modes = xcalloc(argc, sizeof(enum update_mode));
 149        /*
 150         * Keep trailing slash, needed to let
 151         * "git mv file no-such-dir/" error out, except in the case
 152         * "git mv directory no-such-dir/".
 153         */
 154        flags = KEEP_TRAILING_SLASH;
 155        if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
 156                flags = 0;
 157        dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
 158        submodule_gitfile = xcalloc(argc, sizeof(char *));
 159
 160        if (dest_path[0][0] == '\0')
 161                /* special case: "." was normalized to "" */
 162                destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
 163        else if (!lstat(dest_path[0], &st) &&
 164                        S_ISDIR(st.st_mode)) {
 165                dest_path[0] = add_slash(dest_path[0]);
 166                destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
 167        } else {
 168                if (argc != 1)
 169                        die(_("destination '%s' is not a directory"), dest_path[0]);
 170                destination = dest_path;
 171        }
 172
 173        /* Checking */
 174        for (i = 0; i < argc; i++) {
 175                const char *src = source[i], *dst = destination[i];
 176                int length, src_is_dir;
 177                const char *bad = NULL;
 178
 179                if (show_only)
 180                        printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
 181
 182                length = strlen(src);
 183                if (lstat(src, &st) < 0)
 184                        bad = _("bad source");
 185                else if (!strncmp(src, dst, length) &&
 186                                (dst[length] == 0 || dst[length] == '/')) {
 187                        bad = _("can not move directory into itself");
 188                } else if ((src_is_dir = S_ISDIR(st.st_mode))
 189                                && lstat(dst, &st) == 0)
 190                        bad = _("cannot move directory over file");
 191                else if (src_is_dir) {
 192                        int first = cache_name_pos(src, length), last;
 193
 194                        if (first >= 0)
 195                                prepare_move_submodule(src, first,
 196                                                       submodule_gitfile + i);
 197                        else if (index_range_of_same_dir(src, length,
 198                                                         &first, &last) < 1)
 199                                bad = _("source directory is empty");
 200                        else { /* last - first >= 1 */
 201                                int j, dst_len, n;
 202
 203                                modes[i] = WORKING_DIRECTORY;
 204                                n = argc + last - first;
 205                                REALLOC_ARRAY(source, n);
 206                                REALLOC_ARRAY(destination, n);
 207                                REALLOC_ARRAY(modes, n);
 208                                REALLOC_ARRAY(submodule_gitfile, n);
 209
 210                                dst = add_slash(dst);
 211                                dst_len = strlen(dst);
 212
 213                                for (j = 0; j < last - first; j++) {
 214                                        const char *path = active_cache[first + j]->name;
 215                                        source[argc + j] = path;
 216                                        destination[argc + j] =
 217                                                prefix_path(dst, dst_len, path + length + 1);
 218                                        modes[argc + j] = INDEX;
 219                                        submodule_gitfile[argc + j] = NULL;
 220                                }
 221                                argc += last - first;
 222                        }
 223                } else if (cache_name_pos(src, length) < 0)
 224                        bad = _("not under version control");
 225                else if (lstat(dst, &st) == 0 &&
 226                         (!ignore_case || strcasecmp(src, dst))) {
 227                        bad = _("destination exists");
 228                        if (force) {
 229                                /*
 230                                 * only files can overwrite each other:
 231                                 * check both source and destination
 232                                 */
 233                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
 234                                        if (verbose)
 235                                                warning(_("overwriting '%s'"), dst);
 236                                        bad = NULL;
 237                                } else
 238                                        bad = _("Cannot overwrite");
 239                        }
 240                } else if (string_list_has_string(&src_for_dst, dst))
 241                        bad = _("multiple sources for the same target");
 242                else if (is_dir_sep(dst[strlen(dst) - 1]))
 243                        bad = _("destination directory does not exist");
 244                else
 245                        string_list_insert(&src_for_dst, dst);
 246
 247                if (!bad)
 248                        continue;
 249                if (!ignore_errors)
 250                        die(_("%s, source=%s, destination=%s"),
 251                             bad, src, dst);
 252                if (--argc > 0) {
 253                        int n = argc - i;
 254                        memmove(source + i, source + i + 1,
 255                                n * sizeof(char *));
 256                        memmove(destination + i, destination + i + 1,
 257                                n * sizeof(char *));
 258                        memmove(modes + i, modes + i + 1,
 259                                n * sizeof(enum update_mode));
 260                        memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
 261                                n * sizeof(char *));
 262                        i--;
 263                }
 264        }
 265
 266        for (i = 0; i < argc; i++) {
 267                const char *src = source[i], *dst = destination[i];
 268                enum update_mode mode = modes[i];
 269                int pos;
 270                if (show_only || verbose)
 271                        printf(_("Renaming %s to %s\n"), src, dst);
 272                if (show_only)
 273                        continue;
 274                if (mode != INDEX && rename(src, dst) < 0) {
 275                        if (ignore_errors)
 276                                continue;
 277                        die_errno(_("renaming '%s' failed"), src);
 278                }
 279                if (submodule_gitfile[i]) {
 280                        if (!update_path_in_gitmodules(src, dst))
 281                                gitmodules_modified = 1;
 282                        if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
 283                                connect_work_tree_and_git_dir(dst,
 284                                                              submodule_gitfile[i],
 285                                                              1);
 286                }
 287
 288                if (mode == WORKING_DIRECTORY)
 289                        continue;
 290
 291                pos = cache_name_pos(src, strlen(src));
 292                assert(pos >= 0);
 293                rename_cache_entry_at(pos, dst);
 294        }
 295
 296        if (gitmodules_modified)
 297                stage_updated_gitmodules(&the_index);
 298
 299        if (write_locked_index(&the_index, &lock_file,
 300                               COMMIT_LOCK | SKIP_IF_UNCHANGED))
 301                die(_("Unable to write new index file"));
 302
 303        return 0;
 304}