diff: do not use null sha1 as a sentinel value
[git/git.git] / builtin / mv.c
CommitLineData
11be42a4
JS
1/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
11be42a4
JS
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
9#include "cache-tree.h"
c455c87c 10#include "string-list.h"
c7a20c11 11#include "parse-options.h"
11be42a4 12
c7a20c11 13static const char * const builtin_mv_usage[] = {
1b1dd23f 14 "git mv [options] <source>... <destination>",
c7a20c11
PH
15 NULL
16};
11be42a4
JS
17
18static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
20{
d78b0f3d 21 int i;
11be42a4
JS
22 const char **result = xmalloc((count + 1) * sizeof(const char *));
23 memcpy(result, pathspec, count * sizeof(const char *));
24 result[count] = NULL;
d78b0f3d
JS
25 for (i = 0; i < count; i++) {
26 int length = strlen(result[i]);
af82559b
JH
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);
0d0ff65c
BC
32 if (base_name) {
33 result[i] = xstrdup(basename(it));
34 free(it);
35 } else
36 result[i] = it;
af82559b 37 }
11be42a4 38 }
971dfa19 39 return get_pathspec(prefix, result);
11be42a4
JS
40}
41
ac64a722
JS
42static 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);
329a3047
JH
48 with_slash[len++] = '/';
49 with_slash[len] = 0;
ac64a722
JS
50 return with_slash;
51 }
52 return path;
53}
54
11be42a4
JS
55static struct lock_file lock_file;
56
7061cf0f 57int cmd_mv(int argc, const char **argv, const char *prefix)
11be42a4 58{
c7a20c11 59 int i, newfd;
11be42a4 60 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
c7a20c11 61 struct option builtin_mv_options[] = {
07b87389 62 OPT__VERBOSE(&verbose, "be verbose"),
e21adb8c 63 OPT__DRY_RUN(&show_only, "dry run"),
76946b76 64 OPT__FORCE(&force, "force move/rename even if target exists"),
c7a20c11
PH
65 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
66 OPT_END(),
67 };
11be42a4 68 const char **source, **destination, **dest_path;
ac64a722 69 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
11be42a4 70 struct stat st;
183113a5 71 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
11be42a4 72
ef90d6d4 73 git_config(git_default_config, NULL);
11be42a4 74
37782920
SB
75 argc = parse_options(argc, argv, prefix, builtin_mv_options,
76 builtin_mv_usage, 0);
c7a20c11
PH
77 if (--argc < 1)
78 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4 79
99caeed0
JN
80 newfd = hold_locked_index(&lock_file, 1);
81 if (read_cache() < 0)
431b049e 82 die(_("index file corrupt"));
99caeed0 83
c7a20c11
PH
84 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);
11be42a4 87
c5203bdf
JS
88 if (dest_path[0][0] == '\0')
89 /* special case: "." was normalized to "" */
c7a20c11 90 destination = copy_pathspec(dest_path[0], argv, argc, 1);
c5203bdf 91 else if (!lstat(dest_path[0], &st) &&
ac64a722
JS
92 S_ISDIR(st.st_mode)) {
93 dest_path[0] = add_slash(dest_path[0]);
c7a20c11 94 destination = copy_pathspec(dest_path[0], argv, argc, 1);
ac64a722 95 } else {
c7a20c11 96 if (argc != 1)
77471646 97 die("destination '%s' is not a directory", dest_path[0]);
11be42a4
JS
98 destination = dest_path;
99 }
100
101 /* Checking */
c7a20c11 102 for (i = 0; i < argc; i++) {
60a6bf5f
JS
103 const char *src = source[i], *dst = destination[i];
104 int length, src_is_dir;
11be42a4
JS
105 const char *bad = NULL;
106
107 if (show_only)
431b049e 108 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
11be42a4 109
60a6bf5f
JS
110 length = strlen(src);
111 if (lstat(src, &st) < 0)
a7d5629f 112 bad = _("bad source");
60a6bf5f
JS
113 else if (!strncmp(src, dst, length) &&
114 (dst[length] == 0 || dst[length] == '/')) {
a7d5629f 115 bad = _("can not move directory into itself");
60a6bf5f
JS
116 } else if ((src_is_dir = S_ISDIR(st.st_mode))
117 && lstat(dst, &st) == 0)
a7d5629f 118 bad = _("cannot move directory over file");
60a6bf5f 119 else if (src_is_dir) {
aca085e5
JS
120 const char *src_w_slash = add_slash(src);
121 int len_w_slash = length + 1;
60a6bf5f 122 int first, last;
ac64a722
JS
123
124 modes[i] = WORKING_DIRECTORY;
125
aca085e5 126 first = cache_name_pos(src_w_slash, len_w_slash);
ac64a722 127 if (first >= 0)
431b049e 128 die (_("Huh? %.*s is in index?"),
aca085e5 129 len_w_slash, src_w_slash);
ac64a722
JS
130
131 first = -1 - first;
132 for (last = first; last < active_nr; last++) {
133 const char *path = active_cache[last]->name;
aca085e5 134 if (strncmp(path, src_w_slash, len_w_slash))
ac64a722
JS
135 break;
136 }
aca085e5 137 free((char *)src_w_slash);
ac64a722
JS
138
139 if (last - first < 1)
a7d5629f 140 bad = _("source directory is empty");
60a6bf5f
JS
141 else {
142 int j, dst_len;
ac64a722
JS
143
144 if (last - first > 0) {
83572c1a 145 source = xrealloc(source,
c7a20c11 146 (argc + last - first)
ac64a722 147 * sizeof(char *));
83572c1a 148 destination = xrealloc(destination,
c7a20c11 149 (argc + last - first)
ac64a722 150 * sizeof(char *));
83572c1a 151 modes = xrealloc(modes,
c7a20c11 152 (argc + last - first)
ac64a722
JS
153 * sizeof(enum update_mode));
154 }
155
60a6bf5f 156 dst = add_slash(dst);
d089ebaa 157 dst_len = strlen(dst);
ac64a722
JS
158
159 for (j = 0; j < last - first; j++) {
160 const char *path =
161 active_cache[first + j]->name;
c7a20c11
PH
162 source[argc + j] = path;
163 destination[argc + j] =
60a6bf5f 164 prefix_path(dst, dst_len,
d089ebaa 165 path + length + 1);
c7a20c11 166 modes[argc + j] = INDEX;
ac64a722 167 }
c7a20c11 168 argc += last - first;
ac64a722 169 }
5aed3c6a 170 } else if (cache_name_pos(src, length) < 0)
a7d5629f 171 bad = _("not under version control");
5aed3c6a 172 else if (lstat(dst, &st) == 0) {
a7d5629f 173 bad = _("destination exists");
11be42a4
JS
174 if (force) {
175 /*
176 * only files can overwrite each other:
177 * check both source and destination
178 */
81dc2307 179 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
534376ca
JK
180 if (verbose)
181 warning(_("overwriting '%s'"), dst);
11be42a4 182 bad = NULL;
11be42a4 183 } else
a7d5629f 184 bad = _("Cannot overwrite");
11be42a4 185 }
5aed3c6a 186 } else if (string_list_has_string(&src_for_dst, dst))
a7d5629f 187 bad = _("multiple sources for the same target");
60a6bf5f 188 else
78a395d3 189 string_list_insert(&src_for_dst, dst);
11be42a4 190
11be42a4
JS
191 if (bad) {
192 if (ignore_errors) {
c7a20c11 193 if (--argc > 0) {
11be42a4 194 memmove(source + i, source + i + 1,
c7a20c11 195 (argc - i) * sizeof(char *));
11be42a4
JS
196 memmove(destination + i,
197 destination + i + 1,
c7a20c11 198 (argc - i) * sizeof(char *));
be17262d 199 i--;
11be42a4
JS
200 }
201 } else
431b049e 202 die (_("%s, source=%s, destination=%s"),
60a6bf5f 203 bad, src, dst);
11be42a4
JS
204 }
205 }
206
c7a20c11 207 for (i = 0; i < argc; i++) {
60a6bf5f
JS
208 const char *src = source[i], *dst = destination[i];
209 enum update_mode mode = modes[i];
81dc2307 210 int pos;
11be42a4 211 if (show_only || verbose)
431b049e 212 printf(_("Renaming %s to %s\n"), src, dst);
60a6bf5f
JS
213 if (!show_only && mode != INDEX &&
214 rename(src, dst) < 0 && !ignore_errors)
431b049e 215 die_errno (_("renaming '%s' failed"), src);
60a6bf5f
JS
216
217 if (mode == WORKING_DIRECTORY)
ac64a722
JS
218 continue;
219
81dc2307
PB
220 pos = cache_name_pos(src, strlen(src));
221 assert(pos >= 0);
222 if (!show_only)
223 rename_cache_entry_at(pos, dst);
11be42a4
JS
224 }
225
81dc2307
PB
226 if (active_cache_changed) {
227 if (write_cache(newfd, active_cache, active_nr) ||
228 commit_locked_index(&lock_file))
431b049e 229 die(_("Unable to write new index file"));
11be42a4
JS
230 }
231
232 return 0;
233}