builtin/mv.c: plug miniscule memory leak
[git/debian/pape.git] / builtin / mv.c
blob5efe6c5760c43d0059a940ab9d4610b97092c8bd
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "string-list.h"
11 #include "parse-options.h"
13 static const char * const builtin_mv_usage[] = {
14 "git mv [options] <source>... <destination>",
15 NULL
18 static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
21 int i;
22 const char **result = xmalloc((count + 1) * sizeof(const char *));
23 memcpy(result, pathspec, count * sizeof(const char *));
24 result[count] = NULL;
25 for (i = 0; i < count; i++) {
26 int length = strlen(result[i]);
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 if (base_name) {
33 result[i] = xstrdup(basename(it));
34 free(it);
35 } else
36 result[i] = it;
39 return get_pathspec(prefix, result);
42 static const char *add_slash(const char *path)
44 int len = strlen(path);
45 if (path[len - 1] != '/') {
46 char *with_slash = xmalloc(len + 2);
47 memcpy(with_slash, path, len);
48 with_slash[len++] = '/';
49 with_slash[len] = 0;
50 return with_slash;
52 return path;
55 static struct lock_file lock_file;
57 int cmd_mv(int argc, const char **argv, const char *prefix)
59 int i, newfd;
60 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
61 struct option builtin_mv_options[] = {
62 OPT__DRY_RUN(&show_only, "dry run"),
63 OPT__FORCE(&force, "force move/rename even if target exists"),
64 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
65 OPT_END(),
67 const char **source, **destination, **dest_path;
68 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
69 struct stat st;
70 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
72 git_config(git_default_config, NULL);
74 argc = parse_options(argc, argv, prefix, builtin_mv_options,
75 builtin_mv_usage, 0);
76 if (--argc < 1)
77 usage_with_options(builtin_mv_usage, builtin_mv_options);
79 newfd = hold_locked_index(&lock_file, 1);
80 if (read_cache() < 0)
81 die(_("index file corrupt"));
83 source = copy_pathspec(prefix, argv, argc, 0);
84 modes = xcalloc(argc, sizeof(enum update_mode));
85 dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
87 if (dest_path[0][0] == '\0')
88 /* special case: "." was normalized to "" */
89 destination = copy_pathspec(dest_path[0], argv, argc, 1);
90 else if (!lstat(dest_path[0], &st) &&
91 S_ISDIR(st.st_mode)) {
92 dest_path[0] = add_slash(dest_path[0]);
93 destination = copy_pathspec(dest_path[0], argv, argc, 1);
94 } else {
95 if (argc != 1)
96 usage_with_options(builtin_mv_usage, builtin_mv_options);
97 destination = dest_path;
100 /* Checking */
101 for (i = 0; i < argc; i++) {
102 const char *src = source[i], *dst = destination[i];
103 int length, src_is_dir;
104 const char *bad = NULL;
106 if (show_only)
107 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
109 length = strlen(src);
110 if (lstat(src, &st) < 0)
111 bad = _("bad source");
112 else if (!strncmp(src, dst, length) &&
113 (dst[length] == 0 || dst[length] == '/')) {
114 bad = _("can not move directory into itself");
115 } else if ((src_is_dir = S_ISDIR(st.st_mode))
116 && lstat(dst, &st) == 0)
117 bad = _("cannot move directory over file");
118 else if (src_is_dir) {
119 const char *src_w_slash = add_slash(src);
120 int len_w_slash = length + 1;
121 int first, last;
123 modes[i] = WORKING_DIRECTORY;
125 first = cache_name_pos(src_w_slash, len_w_slash);
126 if (first >= 0)
127 die (_("Huh? %.*s is in index?"),
128 len_w_slash, src_w_slash);
130 first = -1 - first;
131 for (last = first; last < active_nr; last++) {
132 const char *path = active_cache[last]->name;
133 if (strncmp(path, src_w_slash, len_w_slash))
134 break;
136 free((char *)src_w_slash);
138 if (last - first < 1)
139 bad = _("source directory is empty");
140 else {
141 int j, dst_len;
143 if (last - first > 0) {
144 source = xrealloc(source,
145 (argc + last - first)
146 * sizeof(char *));
147 destination = xrealloc(destination,
148 (argc + last - first)
149 * sizeof(char *));
150 modes = xrealloc(modes,
151 (argc + last - first)
152 * sizeof(enum update_mode));
155 dst = add_slash(dst);
156 dst_len = strlen(dst);
158 for (j = 0; j < last - first; j++) {
159 const char *path =
160 active_cache[first + j]->name;
161 source[argc + j] = path;
162 destination[argc + j] =
163 prefix_path(dst, dst_len,
164 path + length + 1);
165 modes[argc + j] = INDEX;
167 argc += last - first;
169 } else if (cache_name_pos(src, length) < 0)
170 bad = _("not under version control");
171 else if (lstat(dst, &st) == 0) {
172 bad = _("destination exists");
173 if (force) {
175 * only files can overwrite each other:
176 * check both source and destination
178 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
179 warning(_("%s; will overwrite!"), bad);
180 bad = NULL;
181 } else
182 bad = _("Cannot overwrite");
184 } else if (string_list_has_string(&src_for_dst, dst))
185 bad = _("multiple sources for the same target");
186 else
187 string_list_insert(&src_for_dst, dst);
189 if (bad) {
190 if (ignore_errors) {
191 if (--argc > 0) {
192 memmove(source + i, source + i + 1,
193 (argc - i) * sizeof(char *));
194 memmove(destination + i,
195 destination + i + 1,
196 (argc - i) * sizeof(char *));
197 i--;
199 } else
200 die (_("%s, source=%s, destination=%s"),
201 bad, src, dst);
205 for (i = 0; i < argc; i++) {
206 const char *src = source[i], *dst = destination[i];
207 enum update_mode mode = modes[i];
208 int pos;
209 if (show_only || verbose)
210 printf(_("Renaming %s to %s\n"), src, dst);
211 if (!show_only && mode != INDEX &&
212 rename(src, dst) < 0 && !ignore_errors)
213 die_errno (_("renaming '%s' failed"), src);
215 if (mode == WORKING_DIRECTORY)
216 continue;
218 pos = cache_name_pos(src, strlen(src));
219 assert(pos >= 0);
220 if (!show_only)
221 rename_cache_entry_at(pos, dst);
224 if (active_cache_changed) {
225 if (write_cache(newfd, active_cache, active_nr) ||
226 commit_locked_index(&lock_file))
227 die(_("Unable to write new index file"));
230 return 0;