blob: cdbb09473c0c45efff23a63e78b7623cad523350 [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);
32 result[i] = base_name ? strdup(basename(it)) : it;
33 }
Johannes Schindelin11be42a2006-07-26 03:52:35 +020034 }
Junio C Hamano971dfa12008-03-06 23:29:40 -080035 return get_pathspec(prefix, result);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020036}
37
Johannes Schindelinac64a722006-07-26 19:47:54 +020038static 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 Hamano329a3042006-08-08 12:21:33 -070044 with_slash[len++] = '/';
45 with_slash[len] = 0;
Johannes Schindelinac64a722006-07-26 19:47:54 +020046 return with_slash;
47 }
48 return path;
49}
50
Johannes Schindelin11be42a2006-07-26 03:52:35 +020051static struct lock_file lock_file;
52
Junio C Hamano7061cf02006-07-29 01:54:54 -070053int cmd_mv(int argc, const char **argv, const char *prefix)
Johannes Schindelin11be42a2006-07-26 03:52:35 +020054{
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020055 int i, newfd;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020056 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020057 struct option builtin_mv_options[] = {
58 OPT__DRY_RUN(&show_only),
René Scharfef7aec122009-08-29 11:05:00 +020059 OPT_BOOLEAN('f', "force", &force, "force move/rename even if target exists"),
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020060 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
61 OPT_END(),
62 };
Johannes Schindelin11be42a2006-07-26 03:52:35 +020063 const char **source, **destination, **dest_path;
Johannes Schindelinac64a722006-07-26 19:47:54 +020064 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020065 struct stat st;
Thiago Farina183113a2010-07-04 16:46:19 -030066 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
Johannes Schindelin11be42a2006-07-26 03:52:35 +020067
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010068 git_config(git_default_config, NULL);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020069
Stephen Boyd37782922009-05-23 11:53:12 -070070 argc = parse_options(argc, argv, prefix, builtin_mv_options,
71 builtin_mv_usage, 0);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020072 if (--argc < 1)
73 usage_with_options(builtin_mv_usage, builtin_mv_options);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020074
Jonathan Nieder99caeed2009-11-09 09:05:01 -060075 newfd = hold_locked_index(&lock_file, 1);
76 if (read_cache() < 0)
77 die("index file corrupt");
78
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020079 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 Schindelin11be42a2006-07-26 03:52:35 +020082
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020083 if (dest_path[0][0] == '\0')
84 /* special case: "." was normalized to "" */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020085 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinc5203bd2006-08-18 12:42:39 +020086 else if (!lstat(dest_path[0], &st) &&
Johannes Schindelinac64a722006-07-26 19:47:54 +020087 S_ISDIR(st.st_mode)) {
88 dest_path[0] = add_slash(dest_path[0]);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020089 destination = copy_pathspec(dest_path[0], argv, argc, 1);
Johannes Schindelinac64a722006-07-26 19:47:54 +020090 } else {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020091 if (argc != 1)
92 usage_with_options(builtin_mv_usage, builtin_mv_options);
Johannes Schindelin11be42a2006-07-26 03:52:35 +020093 destination = dest_path;
94 }
95
96 /* Checking */
Pierre Habouzitc7a20c12007-10-07 14:19:33 +020097 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +020098 const char *src = source[i], *dst = destination[i];
99 int length, src_is_dir;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200100 const char *bad = NULL;
101
102 if (show_only)
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200103 printf("Checking rename of '%s' to '%s'\n", src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200104
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200105 length = strlen(src);
106 if (lstat(src, &st) < 0)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200107 bad = "bad source";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200108 else if (!strncmp(src, dst, length) &&
109 (dst[length] == 0 || dst[length] == '/')) {
Johannes Schindelind78b0f32006-08-16 10:44:02 +0200110 bad = "can not move directory into itself";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200111 } 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 Schindelinaca085e2006-12-03 20:42:47 +0100115 const char *src_w_slash = add_slash(src);
116 int len_w_slash = length + 1;
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200117 int first, last;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200118
119 modes[i] = WORKING_DIRECTORY;
120
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100121 first = cache_name_pos(src_w_slash, len_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200122 if (first >= 0)
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100123 die ("Huh? %.*s is in index?",
124 len_w_slash, src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200125
126 first = -1 - first;
127 for (last = first; last < active_nr; last++) {
128 const char *path = active_cache[last]->name;
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100129 if (strncmp(path, src_w_slash, len_w_slash))
Johannes Schindelinac64a722006-07-26 19:47:54 +0200130 break;
131 }
Johannes Schindelinaca085e2006-12-03 20:42:47 +0100132 free((char *)src_w_slash);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200133
134 if (last - first < 1)
135 bad = "source directory is empty";
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200136 else {
137 int j, dst_len;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200138
139 if (last - first > 0) {
Jonas Fonseca83572c12006-08-26 16:16:18 +0200140 source = xrealloc(source,
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 destination = xrealloc(destination,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200144 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200145 * sizeof(char *));
Jonas Fonseca83572c12006-08-26 16:16:18 +0200146 modes = xrealloc(modes,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200147 (argc + last - first)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200148 * sizeof(enum update_mode));
149 }
150
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200151 dst = add_slash(dst);
Junio C Hamanod089eba2008-01-28 22:44:27 -0800152 dst_len = strlen(dst);
Johannes Schindelinac64a722006-07-26 19:47:54 +0200153
154 for (j = 0; j < last - first; j++) {
155 const char *path =
156 active_cache[first + j]->name;
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200157 source[argc + j] = path;
158 destination[argc + j] =
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200159 prefix_path(dst, dst_len,
Junio C Hamanod089eba2008-01-28 22:44:27 -0800160 path + length + 1);
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200161 modes[argc + j] = INDEX;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200162 }
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200163 argc += last - first;
Johannes Schindelinac64a722006-07-26 19:47:54 +0200164 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100165 } else if (cache_name_pos(src, length) < 0)
166 bad = "not under version control";
167 else if (lstat(dst, &st) == 0) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200168 bad = "destination exists";
169 if (force) {
170 /*
171 * only files can overwrite each other:
172 * check both source and destination
173 */
Petr Baudis81dc2302008-07-21 02:25:56 +0200174 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
Thiago Farinabd757c12010-01-03 11:20:30 -0500175 warning("%s; will overwrite!", bad);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200176 bad = NULL;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200177 } else
178 bad = "Cannot overwrite";
179 }
Matthieu Moy5aed3c62009-02-04 10:32:08 +0100180 } else if (string_list_has_string(&src_for_dst, dst))
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200181 bad = "multiple sources for the same target";
182 else
Julian Phillips78a395d2010-06-26 00:41:35 +0100183 string_list_insert(&src_for_dst, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200184
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200185 if (bad) {
186 if (ignore_errors) {
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200187 if (--argc > 0) {
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200188 memmove(source + i, source + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200189 (argc - i) * sizeof(char *));
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200190 memmove(destination + i,
191 destination + i + 1,
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200192 (argc - i) * sizeof(char *));
Michael J Gruberbe172622009-01-14 18:03:22 +0100193 i--;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200194 }
195 } else
Johannes Schindelinac64a722006-07-26 19:47:54 +0200196 die ("%s, source=%s, destination=%s",
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200197 bad, src, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200198 }
199 }
200
Pierre Habouzitc7a20c12007-10-07 14:19:33 +0200201 for (i = 0; i < argc; i++) {
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200202 const char *src = source[i], *dst = destination[i];
203 enum update_mode mode = modes[i];
Petr Baudis81dc2302008-07-21 02:25:56 +0200204 int pos;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200205 if (show_only || verbose)
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200206 printf("Renaming %s to %s\n", src, dst);
207 if (!show_only && mode != INDEX &&
208 rename(src, dst) < 0 && !ignore_errors)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200209 die_errno ("renaming '%s' failed", src);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200210
Johannes Schindelin60a6bf52006-08-19 16:52:21 +0200211 if (mode == WORKING_DIRECTORY)
Johannes Schindelinac64a722006-07-26 19:47:54 +0200212 continue;
213
Petr Baudis81dc2302008-07-21 02:25:56 +0200214 pos = cache_name_pos(src, strlen(src));
215 assert(pos >= 0);
216 if (!show_only)
217 rename_cache_entry_at(pos, dst);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200218 }
219
Petr Baudis81dc2302008-07-21 02:25:56 +0200220 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 Schindelin11be42a2006-07-26 03:52:35 +0200224 }
225
226 return 0;
227}