blob: 2a144b011caa8ecb70f55976bdec60cae89fad9e [file] [log] [blame]
Johannes Schindelin11be42a2006-07-26 03:52:35 +02001/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
Johannes Schindelin11be42a2006-07-26 03:52:35 +02006#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
9#include "cache-tree.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010010#include "string-list.h"
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020011#include "parse-options.h"
Johannes Schindelin11be42a2006-07-26 03:52:35 +020012
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020013static const char * const builtin_mv_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020014 "git mv [options] <source>... <destination>",
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020015 NULL
16};
Johannes Schindelin11be42a2006-07-26 03:52:35 +020017
18static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
20{
Johannes Schindelind78b0f32006-08-16 10:44:02 +020021 int i;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020022 const char **result = xmalloc((count + 1) * sizeof(const char *));
23 memcpy(result, pathspec, count * sizeof(const char *));
24 result[count] = NULL;
Johannes Schindelind78b0f32006-08-16 10:44:02 +020025 for (i = 0; i < count; i++) {
26 int length = strlen(result[i]);
Junio C Hamanoaf825592010-01-22 14:17:06 -080027 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);
Brandon Casey0d0ff652011-10-06 13:22:23 -050032 if (base_name) {
33 result[i] = xstrdup(basename(it));
34 free(it);
35 } else
36 result[i] = it;
Junio C Hamanoaf825592010-01-22 14:17:06 -080037 }
Johannes Schindelin11be42a2006-07-26 03:52:35 +020038 }
Junio C Hamano971dfa12008-03-06 23:29:40 -080039 return get_pathspec(prefix, result);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020040}
41
Johannes Schindelinac64a722006-07-26 19:47:54 +020042static 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);
Junio C Hamano329a3042006-08-08 12:21:33 -070048 with_slash[len++] = '/';
49 with_slash[len] = 0;
Johannes Schindelinac64a722006-07-26 19:47:54 +020050 return with_slash;
51 }
52 return path;
53}
54
Johannes Schindelin11be42a2006-07-26 03:52:35 +020055static struct lock_file lock_file;
56
Junio C Hamano7061cf02006-07-29 01:54:54 -070057int cmd_mv(int argc, const char **argv, const char *prefix)
Johannes Schindelin11be42a2006-07-26 03:52:35 +020058{
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020059 int i, newfd;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020060 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020061 struct option builtin_mv_options[] = {
Jeff King07b87382011-12-12 02:51:24 -050062 OPT__VERBOSE(&verbose, "be verbose"),
René Scharfee21adb82010-11-08 18:58:51 +010063 OPT__DRY_RUN(&show_only, "dry run"),
René Scharfe76946b72010-11-08 19:01:54 +010064 OPT__FORCE(&force, "force move/rename even if target exists"),
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020065 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
66 OPT_END(),
67 };
Johannes Schindelin11be42a2006-07-26 03:52:35 +020068 const char **source, **destination, **dest_path;
Johannes Schindelinac64a722006-07-26 19:47:54 +020069 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020070 struct stat st;
Thiago Farina183113a2010-07-04 16:46:19 -030071 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020072
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010073 git_config(git_default_config, NULL);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020074
Stephen Boyd37782922009-05-23 11:53:12 -070075 argc = parse_options(argc, argv, prefix, builtin_mv_options,
76 builtin_mv_usage, 0);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020077 if (--argc < 1)
78 usage_with_options(builtin_mv_usage, builtin_mv_options);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020079
Jonathan Nieder99caeed2009-11-09 09:05:01 -060080 newfd = hold_locked_index(&lock_file, 1);
81 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +000082 die(_("index file corrupt"));
Jonathan Nieder99caeed2009-11-09 09:05:01 -060083
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020084 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);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020087
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020088 if (dest_path[0][0] == '\0')
89 /* special case: "." was normalized to "" */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020090 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020091 else if (!lstat(dest_path[0], &st) &&
Johannes Schindelinac64a722006-07-26 19:47:54 +020092 S_ISDIR(st.st_mode)) {
93 dest_path[0] = add_slash(dest_path[0]);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020094 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinac64a722006-07-26 19:47:54 +020095 } else {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020096 if (argc != 1)
Jeff King77471642011-12-12 02:51:36 -050097 die("destination '%s' is not a directory", dest_path[0]);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020098 destination = dest_path;
99 }
100
101 /* Checking */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200102 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200103 const char *src = source[i], *dst = destination[i];
104 int length, src_is_dir;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200105 const char *bad = NULL;
106
107 if (show_only)
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000108 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200109
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200110 length = strlen(src);
111 if (lstat(src, &st) < 0)
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000112 bad = _("bad source");
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200113 else if (!strncmp(src, dst, length) &&
114 (dst[length] == 0 || dst[length] == '/')) {
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000115 bad = _("can not move directory into itself");
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200116 } else if ((src_is_dir = S_ISDIR(st.st_mode))
117 && lstat(dst, &st) == 0)
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000118 bad = _("cannot move directory over file");
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200119 else if (src_is_dir) {
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100120 const char *src_w_slash = add_slash(src);
121 int len_w_slash = length + 1;
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200122 int first, last;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200123
124 modes[i] = WORKING_DIRECTORY;
125
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100126 first = cache_name_pos(src_w_slash, len_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200127 if (first >= 0)
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000128 die (_("Huh? %.*s is in index?"),
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100129 len_w_slash, src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200130
131 first = -1 - first;
132 for (last = first; last < active_nr; last++) {
133 const char *path = active_cache[last]->name;
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100134 if (strncmp(path, src_w_slash, len_w_slash))
Johannes Schindelinac64a722006-07-26 19:47:54 +0200135 break;
136 }
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100137 free((char *)src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200138
139 if (last - first < 1)
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000140 bad = _("source directory is empty");
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200141 else {
142 int j, dst_len;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200143
144 if (last - first > 0) {
Jonas Fonseca83572c12006-08-26 16:16:18 +0200145 source = xrealloc(source,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200146 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200147 * sizeof(char *));
Jonas Fonseca83572c12006-08-26 16:16:18 +0200148 destination = xrealloc(destination,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200149 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200150 * sizeof(char *));
Jonas Fonseca83572c12006-08-26 16:16:18 +0200151 modes = xrealloc(modes,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200152 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200153 * sizeof(enum update_mode));
154 }
155
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200156 dst = add_slash(dst);
Junio C Hamanod089eba2008-01-28 22:44:27 -0800157 dst_len = strlen(dst);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200158
159 for (j = 0; j < last - first; j++) {
160 const char *path =
161 active_cache[first + j]->name;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200162 source[argc + j] = path;
163 destination[argc + j] =
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200164 prefix_path(dst, dst_len,
Junio C Hamanod089eba2008-01-28 22:44:27 -0800165 path + length + 1);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200166 modes[argc + j] = INDEX;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200167 }
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200168 argc += last - first;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200169 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100170 } else if (cache_name_pos(src, length) < 0)
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000171 bad = _("not under version control");
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100172 else if (lstat(dst, &st) == 0) {
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000173 bad = _("destination exists");
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200174 if (force) {
175 /*
176 * only files can overwrite each other:
177 * check both source and destination
178 */
Petr Baudis81dc2302008-07-21 02:25:56 +0200179 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
Jeff King534376c2011-12-12 16:54:42 -0500180 if (verbose)
181 warning(_("overwriting '%s'"), dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200182 bad = NULL;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200183 } else
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000184 bad = _("Cannot overwrite");
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200185 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100186 } else if (string_list_has_string(&src_for_dst, dst))
Ævar Arnfjörð Bjarmasona7d56292011-02-22 23:42:04 +0000187 bad = _("multiple sources for the same target");
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200188 else
Julian Phillips78a395d2010-06-26 00:41:35 +0100189 string_list_insert(&src_for_dst, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200190
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200191 if (bad) {
192 if (ignore_errors) {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200193 if (--argc > 0) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200194 memmove(source + i, source + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200195 (argc - i) * sizeof(char *));
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200196 memmove(destination + i,
197 destination + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200198 (argc - i) * sizeof(char *));
Michael J Gruberbe172622009-01-14 18:03:22 +0100199 i--;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200200 }
201 } else
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000202 die (_("%s, source=%s, destination=%s"),
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200203 bad, src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200204 }
205 }
206
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200207 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200208 const char *src = source[i], *dst = destination[i];
209 enum update_mode mode = modes[i];
Petr Baudis81dc2302008-07-21 02:25:56 +0200210 int pos;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200211 if (show_only || verbose)
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000212 printf(_("Renaming %s to %s\n"), src, dst);
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200213 if (!show_only && mode != INDEX &&
214 rename(src, dst) < 0 && !ignore_errors)
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000215 die_errno (_("renaming '%s' failed"), src);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200216
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200217 if (mode == WORKING_DIRECTORY)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200218 continue;
219
Petr Baudis81dc2302008-07-21 02:25:56 +0200220 pos = cache_name_pos(src, strlen(src));
221 assert(pos >= 0);
222 if (!show_only)
223 rename_cache_entry_at(pos, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200224 }
225
Petr Baudis81dc2302008-07-21 02:25:56 +0200226 if (active_cache_changed) {
227 if (write_cache(newfd, active_cache, active_nr) ||
228 commit_locked_index(&lock_file))
Ævar Arnfjörð Bjarmason431b0492011-02-22 23:42:03 +0000229 die(_("Unable to write new index file"));
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200230 }
231
232 return 0;
233}