blob: f633d81424f5e41cb85ac660dbdc9f4913852673 [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]);
Johannes Sixtb8f26262009-06-30 15:33:57 +020027 if (length > 0 && is_dir_sep(result[i][length - 1]))
Pierre Habouzit182af832007-09-16 00:32:36 +020028 result[i] = xmemdupz(result[i], length - 1);
Johannes Sixtb8f26262009-06-30 15:33:57 +020029 if (base_name)
30 result[i] = basename((char *)result[i]);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020031 }
Junio C Hamano971dfa12008-03-06 23:29:40 -080032 return get_pathspec(prefix, result);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020033}
34
Johannes Schindelinac64a722006-07-26 19:47:54 +020035static const char *add_slash(const char *path)
36{
37 int len = strlen(path);
38 if (path[len - 1] != '/') {
39 char *with_slash = xmalloc(len + 2);
40 memcpy(with_slash, path, len);
Junio C Hamano329a3042006-08-08 12:21:33 -070041 with_slash[len++] = '/';
42 with_slash[len] = 0;
Johannes Schindelinac64a722006-07-26 19:47:54 +020043 return with_slash;
44 }
45 return path;
46}
47
Johannes Schindelin11be42a2006-07-26 03:52:35 +020048static struct lock_file lock_file;
49
Junio C Hamano7061cf02006-07-29 01:54:54 -070050int cmd_mv(int argc, const char **argv, const char *prefix)
Johannes Schindelin11be42a2006-07-26 03:52:35 +020051{
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020052 int i, newfd;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020053 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020054 struct option builtin_mv_options[] = {
55 OPT__DRY_RUN(&show_only),
René Scharfef7aec122009-08-29 11:05:00 +020056 OPT_BOOLEAN('f', "force", &force, "force move/rename even if target exists"),
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020057 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
58 OPT_END(),
59 };
Johannes Schindelin11be42a2006-07-26 03:52:35 +020060 const char **source, **destination, **dest_path;
Johannes Schindelinac64a722006-07-26 19:47:54 +020061 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020062 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +010063 struct string_list src_for_dst = {NULL, 0, 0, 0};
Johannes Schindelin11be42a2006-07-26 03:52:35 +020064
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010065 git_config(git_default_config, NULL);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020066
Stephen Boyd37782922009-05-23 11:53:12 -070067 argc = parse_options(argc, argv, prefix, builtin_mv_options,
68 builtin_mv_usage, 0);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020069 if (--argc < 1)
70 usage_with_options(builtin_mv_usage, builtin_mv_options);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020071
Jonathan Nieder99caeed2009-11-09 09:05:01 -060072 newfd = hold_locked_index(&lock_file, 1);
73 if (read_cache() < 0)
74 die("index file corrupt");
75
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020076 source = copy_pathspec(prefix, argv, argc, 0);
77 modes = xcalloc(argc, sizeof(enum update_mode));
78 dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020079
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020080 if (dest_path[0][0] == '\0')
81 /* special case: "." was normalized to "" */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020082 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020083 else if (!lstat(dest_path[0], &st) &&
Johannes Schindelinac64a722006-07-26 19:47:54 +020084 S_ISDIR(st.st_mode)) {
85 dest_path[0] = add_slash(dest_path[0]);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020086 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinac64a722006-07-26 19:47:54 +020087 } else {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020088 if (argc != 1)
89 usage_with_options(builtin_mv_usage, builtin_mv_options);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020090 destination = dest_path;
91 }
92
93 /* Checking */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020094 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +020095 const char *src = source[i], *dst = destination[i];
96 int length, src_is_dir;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020097 const char *bad = NULL;
98
99 if (show_only)
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200100 printf("Checking rename of '%s' to '%s'\n", src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200101
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200102 length = strlen(src);
103 if (lstat(src, &st) < 0)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200104 bad = "bad source";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200105 else if (!strncmp(src, dst, length) &&
106 (dst[length] == 0 || dst[length] == '/')) {
Johannes Schindelind78b0f32006-08-16 10:44:02 +0200107 bad = "can not move directory into itself";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200108 } else if ((src_is_dir = S_ISDIR(st.st_mode))
109 && lstat(dst, &st) == 0)
110 bad = "cannot move directory over file";
111 else if (src_is_dir) {
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100112 const char *src_w_slash = add_slash(src);
113 int len_w_slash = length + 1;
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200114 int first, last;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200115
116 modes[i] = WORKING_DIRECTORY;
117
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100118 first = cache_name_pos(src_w_slash, len_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200119 if (first >= 0)
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100120 die ("Huh? %.*s is in index?",
121 len_w_slash, src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200122
123 first = -1 - first;
124 for (last = first; last < active_nr; last++) {
125 const char *path = active_cache[last]->name;
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100126 if (strncmp(path, src_w_slash, len_w_slash))
Johannes Schindelinac64a722006-07-26 19:47:54 +0200127 break;
128 }
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100129 free((char *)src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200130
131 if (last - first < 1)
132 bad = "source directory is empty";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200133 else {
134 int j, dst_len;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200135
136 if (last - first > 0) {
Jonas Fonseca83572c12006-08-26 16:16:18 +0200137 source = xrealloc(source,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200138 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200139 * sizeof(char *));
Jonas Fonseca83572c12006-08-26 16:16:18 +0200140 destination = xrealloc(destination,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200141 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200142 * sizeof(char *));
Jonas Fonseca83572c12006-08-26 16:16:18 +0200143 modes = xrealloc(modes,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200144 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200145 * sizeof(enum update_mode));
146 }
147
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200148 dst = add_slash(dst);
Junio C Hamanod089eba2008-01-28 22:44:27 -0800149 dst_len = strlen(dst);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200150
151 for (j = 0; j < last - first; j++) {
152 const char *path =
153 active_cache[first + j]->name;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200154 source[argc + j] = path;
155 destination[argc + j] =
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200156 prefix_path(dst, dst_len,
Junio C Hamanod089eba2008-01-28 22:44:27 -0800157 path + length + 1);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200158 modes[argc + j] = INDEX;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200159 }
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200160 argc += last - first;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200161 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100162 } else if (cache_name_pos(src, length) < 0)
163 bad = "not under version control";
164 else if (lstat(dst, &st) == 0) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200165 bad = "destination exists";
166 if (force) {
167 /*
168 * only files can overwrite each other:
169 * check both source and destination
170 */
Petr Baudis81dc2302008-07-21 02:25:56 +0200171 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200172 fprintf(stderr, "Warning: %s;"
173 " will overwrite!\n",
174 bad);
175 bad = NULL;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200176 } else
177 bad = "Cannot overwrite";
178 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100179 } else if (string_list_has_string(&src_for_dst, dst))
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200180 bad = "multiple sources for the same target";
181 else
Johannes Schindelinc455c872008-07-21 19:03:49 +0100182 string_list_insert(dst, &src_for_dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200183
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200184 if (bad) {
185 if (ignore_errors) {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200186 if (--argc > 0) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200187 memmove(source + i, source + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200188 (argc - i) * sizeof(char *));
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200189 memmove(destination + i,
190 destination + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200191 (argc - i) * sizeof(char *));
Michael J Gruberbe172622009-01-14 18:03:22 +0100192 i--;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200193 }
194 } else
Johannes Schindelinac64a722006-07-26 19:47:54 +0200195 die ("%s, source=%s, destination=%s",
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200196 bad, src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200197 }
198 }
199
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200200 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200201 const char *src = source[i], *dst = destination[i];
202 enum update_mode mode = modes[i];
Petr Baudis81dc2302008-07-21 02:25:56 +0200203 int pos;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200204 if (show_only || verbose)
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200205 printf("Renaming %s to %s\n", src, dst);
206 if (!show_only && mode != INDEX &&
207 rename(src, dst) < 0 && !ignore_errors)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200208 die_errno ("renaming '%s' failed", src);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200209
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200210 if (mode == WORKING_DIRECTORY)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200211 continue;
212
Petr Baudis81dc2302008-07-21 02:25:56 +0200213 pos = cache_name_pos(src, strlen(src));
214 assert(pos >= 0);
215 if (!show_only)
216 rename_cache_entry_at(pos, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200217 }
218
Petr Baudis81dc2302008-07-21 02:25:56 +0200219 if (active_cache_changed) {
220 if (write_cache(newfd, active_cache, active_nr) ||
221 commit_locked_index(&lock_file))
222 die("Unable to write new index file");
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200223 }
224
225 return 0;
226}