Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * "git mv" builtin command |
| 3 | * |
| 4 | * Copyright (C) 2006 Johannes Schindelin |
| 5 | */ |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "builtin.h" |
| 8 | #include "dir.h" |
| 9 | #include "cache-tree.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 10 | #include "string-list.h" |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 12 | |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 13 | static const char * const builtin_mv_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 14 | "git mv [options] <source>... <destination>", |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 15 | NULL |
| 16 | }; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 17 | |
| 18 | static const char **copy_pathspec(const char *prefix, const char **pathspec, |
| 19 | int count, int base_name) |
| 20 | { |
Johannes Schindelin | d78b0f3 | 2006-08-16 10:44:02 +0200 | [diff] [blame] | 21 | int i; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 22 | const char **result = xmalloc((count + 1) * sizeof(const char *)); |
| 23 | memcpy(result, pathspec, count * sizeof(const char *)); |
| 24 | result[count] = NULL; |
Johannes Schindelin | d78b0f3 | 2006-08-16 10:44:02 +0200 | [diff] [blame] | 25 | for (i = 0; i < count; i++) { |
| 26 | int length = strlen(result[i]); |
Junio C Hamano | af82559 | 2010-01-22 14:17:06 -0800 | [diff] [blame] | 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 | result[i] = base_name ? strdup(basename(it)) : it; |
| 33 | } |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 34 | } |
Junio C Hamano | 971dfa1 | 2008-03-06 23:29:40 -0800 | [diff] [blame] | 35 | return get_pathspec(prefix, result); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 38 | static const char *add_slash(const char *path) |
| 39 | { |
| 40 | int len = strlen(path); |
| 41 | if (path[len - 1] != '/') { |
| 42 | char *with_slash = xmalloc(len + 2); |
| 43 | memcpy(with_slash, path, len); |
Junio C Hamano | 329a304 | 2006-08-08 12:21:33 -0700 | [diff] [blame] | 44 | with_slash[len++] = '/'; |
| 45 | with_slash[len] = 0; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 46 | return with_slash; |
| 47 | } |
| 48 | return path; |
| 49 | } |
| 50 | |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 51 | static struct lock_file lock_file; |
| 52 | |
Junio C Hamano | 7061cf0 | 2006-07-29 01:54:54 -0700 | [diff] [blame] | 53 | int cmd_mv(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 54 | { |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 55 | int i, newfd; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 56 | int verbose = 0, show_only = 0, force = 0, ignore_errors = 0; |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 57 | struct option builtin_mv_options[] = { |
| 58 | OPT__DRY_RUN(&show_only), |
René Scharfe | f7aec12 | 2009-08-29 11:05:00 +0200 | [diff] [blame] | 59 | OPT_BOOLEAN('f', "force", &force, "force move/rename even if target exists"), |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 60 | OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"), |
| 61 | OPT_END(), |
| 62 | }; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 63 | const char **source, **destination, **dest_path; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 64 | enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 65 | struct stat st; |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 66 | struct string_list src_for_dst = STRING_LIST_INIT_NODUP; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 67 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 68 | git_config(git_default_config, NULL); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 69 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 70 | argc = parse_options(argc, argv, prefix, builtin_mv_options, |
| 71 | builtin_mv_usage, 0); |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 72 | if (--argc < 1) |
| 73 | usage_with_options(builtin_mv_usage, builtin_mv_options); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 74 | |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 75 | newfd = hold_locked_index(&lock_file, 1); |
| 76 | if (read_cache() < 0) |
| 77 | die("index file corrupt"); |
| 78 | |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 79 | source = copy_pathspec(prefix, argv, argc, 0); |
| 80 | modes = xcalloc(argc, sizeof(enum update_mode)); |
| 81 | dest_path = copy_pathspec(prefix, argv + argc, 1, 0); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 82 | |
Johannes Schindelin | c5203bd | 2006-08-18 12:42:39 +0200 | [diff] [blame] | 83 | if (dest_path[0][0] == '\0') |
| 84 | /* special case: "." was normalized to "" */ |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 85 | destination = copy_pathspec(dest_path[0], argv, argc, 1); |
Johannes Schindelin | c5203bd | 2006-08-18 12:42:39 +0200 | [diff] [blame] | 86 | else if (!lstat(dest_path[0], &st) && |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 87 | S_ISDIR(st.st_mode)) { |
| 88 | dest_path[0] = add_slash(dest_path[0]); |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 89 | destination = copy_pathspec(dest_path[0], argv, argc, 1); |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 90 | } else { |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 91 | if (argc != 1) |
| 92 | usage_with_options(builtin_mv_usage, builtin_mv_options); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 93 | destination = dest_path; |
| 94 | } |
| 95 | |
| 96 | /* Checking */ |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 97 | for (i = 0; i < argc; i++) { |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 98 | const char *src = source[i], *dst = destination[i]; |
| 99 | int length, src_is_dir; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 100 | const char *bad = NULL; |
| 101 | |
| 102 | if (show_only) |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 103 | printf("Checking rename of '%s' to '%s'\n", src, dst); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 104 | |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 105 | length = strlen(src); |
| 106 | if (lstat(src, &st) < 0) |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 107 | bad = "bad source"; |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 108 | else if (!strncmp(src, dst, length) && |
| 109 | (dst[length] == 0 || dst[length] == '/')) { |
Johannes Schindelin | d78b0f3 | 2006-08-16 10:44:02 +0200 | [diff] [blame] | 110 | bad = "can not move directory into itself"; |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 111 | } else if ((src_is_dir = S_ISDIR(st.st_mode)) |
| 112 | && lstat(dst, &st) == 0) |
| 113 | bad = "cannot move directory over file"; |
| 114 | else if (src_is_dir) { |
Johannes Schindelin | aca085e | 2006-12-03 20:42:47 +0100 | [diff] [blame] | 115 | const char *src_w_slash = add_slash(src); |
| 116 | int len_w_slash = length + 1; |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 117 | int first, last; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 118 | |
| 119 | modes[i] = WORKING_DIRECTORY; |
| 120 | |
Johannes Schindelin | aca085e | 2006-12-03 20:42:47 +0100 | [diff] [blame] | 121 | first = cache_name_pos(src_w_slash, len_w_slash); |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 122 | if (first >= 0) |
Johannes Schindelin | aca085e | 2006-12-03 20:42:47 +0100 | [diff] [blame] | 123 | die ("Huh? %.*s is in index?", |
| 124 | len_w_slash, src_w_slash); |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 125 | |
| 126 | first = -1 - first; |
| 127 | for (last = first; last < active_nr; last++) { |
| 128 | const char *path = active_cache[last]->name; |
Johannes Schindelin | aca085e | 2006-12-03 20:42:47 +0100 | [diff] [blame] | 129 | if (strncmp(path, src_w_slash, len_w_slash)) |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 130 | break; |
| 131 | } |
Johannes Schindelin | aca085e | 2006-12-03 20:42:47 +0100 | [diff] [blame] | 132 | free((char *)src_w_slash); |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 133 | |
| 134 | if (last - first < 1) |
| 135 | bad = "source directory is empty"; |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 136 | else { |
| 137 | int j, dst_len; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 138 | |
| 139 | if (last - first > 0) { |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 140 | source = xrealloc(source, |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 141 | (argc + last - first) |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 142 | * sizeof(char *)); |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 143 | destination = xrealloc(destination, |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 144 | (argc + last - first) |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 145 | * sizeof(char *)); |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 146 | modes = xrealloc(modes, |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 147 | (argc + last - first) |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 148 | * sizeof(enum update_mode)); |
| 149 | } |
| 150 | |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 151 | dst = add_slash(dst); |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 152 | dst_len = strlen(dst); |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 153 | |
| 154 | for (j = 0; j < last - first; j++) { |
| 155 | const char *path = |
| 156 | active_cache[first + j]->name; |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 157 | source[argc + j] = path; |
| 158 | destination[argc + j] = |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 159 | prefix_path(dst, dst_len, |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 160 | path + length + 1); |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 161 | modes[argc + j] = INDEX; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 162 | } |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 163 | argc += last - first; |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 164 | } |
Matthieu Moy | 5aed3c6 | 2009-02-04 10:32:08 +0100 | [diff] [blame] | 165 | } else if (cache_name_pos(src, length) < 0) |
| 166 | bad = "not under version control"; |
| 167 | else if (lstat(dst, &st) == 0) { |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 168 | bad = "destination exists"; |
| 169 | if (force) { |
| 170 | /* |
| 171 | * only files can overwrite each other: |
| 172 | * check both source and destination |
| 173 | */ |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 174 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
Thiago Farina | bd757c1 | 2010-01-03 11:20:30 -0500 | [diff] [blame] | 175 | warning("%s; will overwrite!", bad); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 176 | bad = NULL; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 177 | } else |
| 178 | bad = "Cannot overwrite"; |
| 179 | } |
Matthieu Moy | 5aed3c6 | 2009-02-04 10:32:08 +0100 | [diff] [blame] | 180 | } else if (string_list_has_string(&src_for_dst, dst)) |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 181 | bad = "multiple sources for the same target"; |
| 182 | else |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 183 | string_list_insert(&src_for_dst, dst); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 184 | |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 185 | if (bad) { |
| 186 | if (ignore_errors) { |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 187 | if (--argc > 0) { |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 188 | memmove(source + i, source + i + 1, |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 189 | (argc - i) * sizeof(char *)); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 190 | memmove(destination + i, |
| 191 | destination + i + 1, |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 192 | (argc - i) * sizeof(char *)); |
Michael J Gruber | be17262 | 2009-01-14 18:03:22 +0100 | [diff] [blame] | 193 | i--; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 194 | } |
| 195 | } else |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 196 | die ("%s, source=%s, destination=%s", |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 197 | bad, src, dst); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Pierre Habouzit | c7a20c1 | 2007-10-07 14:19:33 +0200 | [diff] [blame] | 201 | for (i = 0; i < argc; i++) { |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 202 | const char *src = source[i], *dst = destination[i]; |
| 203 | enum update_mode mode = modes[i]; |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 204 | int pos; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 205 | if (show_only || verbose) |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 206 | printf("Renaming %s to %s\n", src, dst); |
| 207 | if (!show_only && mode != INDEX && |
| 208 | rename(src, dst) < 0 && !ignore_errors) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 209 | die_errno ("renaming '%s' failed", src); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 210 | |
Johannes Schindelin | 60a6bf5 | 2006-08-19 16:52:21 +0200 | [diff] [blame] | 211 | if (mode == WORKING_DIRECTORY) |
Johannes Schindelin | ac64a72 | 2006-07-26 19:47:54 +0200 | [diff] [blame] | 212 | continue; |
| 213 | |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 214 | pos = cache_name_pos(src, strlen(src)); |
| 215 | assert(pos >= 0); |
| 216 | if (!show_only) |
| 217 | rename_cache_entry_at(pos, dst); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 220 | if (active_cache_changed) { |
| 221 | if (write_cache(newfd, active_cache, active_nr) || |
| 222 | commit_locked_index(&lock_file)) |
| 223 | die("Unable to write new index file"); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |