2  * "git mv" builtin command
 
   4  * Copyright (C) 2006 Johannes Schindelin
 
   9 #include "cache-tree.h"
 
  10 #include "string-list.h"
 
  11 #include "parse-options.h"
 
  13 static const char * const builtin_mv_usage[] = {
 
  14         "git mv [options] <source>... <destination>",
 
  18 static const char **copy_pathspec(const char *prefix, const char **pathspec,
 
  19                                   int count, int base_name)
 
  22         const char **result = xmalloc((count + 1) * sizeof(const char *));
 
  23         memcpy(result, pathspec, count * sizeof(const char *));
 
  25         for (i = 0; i < count; i++) {
 
  26                 int length = strlen(result[i]);
 
  28                 while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
 
  30                 if (to_copy != length || base_name) {
 
  31                         char *it = xmemdupz(result[i], to_copy);
 
  33                                 result[i] = xstrdup(basename(it));
 
  39         return get_pathspec(prefix, result);
 
  42 static const char *add_slash(const char *path)
 
  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++] = '/';
 
  55 static struct lock_file lock_file;
 
  57 int cmd_mv(int argc, const char **argv, const char *prefix)
 
  60         int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
 
  61         struct option builtin_mv_options[] = {
 
  62                 OPT__DRY_RUN(&show_only, "dry run"),
 
  63                 OPT__FORCE(&force, "force move/rename even if target exists"),
 
  64                 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
 
  67         const char **source, **destination, **dest_path;
 
  68         enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
 
  70         struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 
  72         git_config(git_default_config, NULL);
 
  74         argc = parse_options(argc, argv, prefix, builtin_mv_options,
 
  77                 usage_with_options(builtin_mv_usage, builtin_mv_options);
 
  79         newfd = hold_locked_index(&lock_file, 1);
 
  81                 die(_("index file corrupt"));
 
  83         source = copy_pathspec(prefix, argv, argc, 0);
 
  84         modes = xcalloc(argc, sizeof(enum update_mode));
 
  85         dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
 
  87         if (dest_path[0][0] == '\0')
 
  88                 /* special case: "." was normalized to "" */
 
  89                 destination = copy_pathspec(dest_path[0], argv, argc, 1);
 
  90         else if (!lstat(dest_path[0], &st) &&
 
  91                         S_ISDIR(st.st_mode)) {
 
  92                 dest_path[0] = add_slash(dest_path[0]);
 
  93                 destination = copy_pathspec(dest_path[0], argv, argc, 1);
 
  96                         usage_with_options(builtin_mv_usage, builtin_mv_options);
 
  97                 destination = dest_path;
 
 101         for (i = 0; i < argc; i++) {
 
 102                 const char *src = source[i], *dst = destination[i];
 
 103                 int length, src_is_dir;
 
 104                 const char *bad = NULL;
 
 107                         printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
 
 109                 length = strlen(src);
 
 110                 if (lstat(src, &st) < 0)
 
 111                         bad = _("bad source");
 
 112                 else if (!strncmp(src, dst, length) &&
 
 113                                 (dst[length] == 0 || dst[length] == '/')) {
 
 114                         bad = _("can not move directory into itself");
 
 115                 } else if ((src_is_dir = S_ISDIR(st.st_mode))
 
 116                                 && lstat(dst, &st) == 0)
 
 117                         bad = _("cannot move directory over file");
 
 118                 else if (src_is_dir) {
 
 119                         const char *src_w_slash = add_slash(src);
 
 120                         int len_w_slash = length + 1;
 
 123                         modes[i] = WORKING_DIRECTORY;
 
 125                         first = cache_name_pos(src_w_slash, len_w_slash);
 
 127                                 die (_("Huh? %.*s is in index?"),
 
 128                                                 len_w_slash, src_w_slash);
 
 131                         for (last = first; last < active_nr; last++) {
 
 132                                 const char *path = active_cache[last]->name;
 
 133                                 if (strncmp(path, src_w_slash, len_w_slash))
 
 136                         free((char *)src_w_slash);
 
 138                         if (last - first < 1)
 
 139                                 bad = _("source directory is empty");
 
 143                                 if (last - first > 0) {
 
 144                                         source = xrealloc(source,
 
 145                                                         (argc + last - first)
 
 147                                         destination = xrealloc(destination,
 
 148                                                         (argc + last - first)
 
 150                                         modes = xrealloc(modes,
 
 151                                                         (argc + last - first)
 
 152                                                         * sizeof(enum update_mode));
 
 155                                 dst = add_slash(dst);
 
 156                                 dst_len = strlen(dst);
 
 158                                 for (j = 0; j < last - first; j++) {
 
 160                                                 active_cache[first + j]->name;
 
 161                                         source[argc + j] = path;
 
 162                                         destination[argc + j] =
 
 163                                                 prefix_path(dst, dst_len,
 
 165                                         modes[argc + j] = INDEX;
 
 167                                 argc += last - first;
 
 169                 } else if (cache_name_pos(src, length) < 0)
 
 170                         bad = _("not under version control");
 
 171                 else if (lstat(dst, &st) == 0) {
 
 172                         bad = _("destination exists");
 
 175                                  * only files can overwrite each other:
 
 176                                  * check both source and destination
 
 178                                 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
 
 179                                         warning(_("%s; will overwrite!"), bad);
 
 182                                         bad = _("Cannot overwrite");
 
 184                 } else if (string_list_has_string(&src_for_dst, dst))
 
 185                         bad = _("multiple sources for the same target");
 
 187                         string_list_insert(&src_for_dst, dst);
 
 192                                         memmove(source + i, source + i + 1,
 
 193                                                 (argc - i) * sizeof(char *));
 
 194                                         memmove(destination + i,
 
 196                                                 (argc - i) * sizeof(char *));
 
 200                                 die (_("%s, source=%s, destination=%s"),
 
 205         for (i = 0; i < argc; i++) {
 
 206                 const char *src = source[i], *dst = destination[i];
 
 207                 enum update_mode mode = modes[i];
 
 209                 if (show_only || verbose)
 
 210                         printf(_("Renaming %s to %s\n"), src, dst);
 
 211                 if (!show_only && mode != INDEX &&
 
 212                                 rename(src, dst) < 0 && !ignore_errors)
 
 213                         die_errno (_("renaming '%s' failed"), src);
 
 215                 if (mode == WORKING_DIRECTORY)
 
 218                 pos = cache_name_pos(src, strlen(src));
 
 221                         rename_cache_entry_at(pos, dst);
 
 224         if (active_cache_changed) {
 
 225                 if (write_cache(newfd, active_cache, active_nr) ||
 
 226                     commit_locked_index(&lock_file))
 
 227                         die(_("Unable to write new index file"));