2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
11 #include "cache-tree.h"
12 #include "path-list.h"
14 static const char builtin_mv_usage
[] =
15 "git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
17 static const char **copy_pathspec(const char *prefix
, const char **pathspec
,
18 int count
, int base_name
)
21 const char **result
= xmalloc((count
+ 1) * sizeof(const char *));
22 memcpy(result
, pathspec
, count
* sizeof(const char *));
24 for (i
= 0; i
< count
; i
++) {
25 int length
= strlen(result
[i
]);
26 if (length
> 0 && result
[i
][length
- 1] == '/') {
27 char *without_slash
= xmalloc(length
);
28 memcpy(without_slash
, result
[i
], length
- 1);
29 without_slash
[length
] = '\0';
30 result
[i
] = without_slash
;
33 const char *last_slash
= strrchr(result
[i
], '/');
35 result
[i
] = last_slash
+ 1;
38 return get_pathspec(prefix
, result
);
41 static void show_list(const char *label
, struct path_list
*list
)
46 for (i
= 0; i
< list
->nr
; i
++)
47 printf("%s%s", i
> 0 ? ", " : "", list
->items
[i
].path
);
52 static const char *add_slash(const char *path
)
54 int len
= strlen(path
);
55 if (path
[len
- 1] != '/') {
56 char *with_slash
= xmalloc(len
+ 2);
57 memcpy(with_slash
, path
, len
);
58 with_slash
[len
++] = '/';
65 static struct lock_file lock_file
;
67 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
70 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
71 const char **source
, **destination
, **dest_path
;
72 enum update_mode
{ BOTH
= 0, WORKING_DIRECTORY
, INDEX
} *modes
;
74 struct path_list overwritten
= {NULL
, 0, 0, 0};
75 struct path_list src_for_dst
= {NULL
, 0, 0, 0};
76 struct path_list added
= {NULL
, 0, 0, 0};
77 struct path_list deleted
= {NULL
, 0, 0, 0};
78 struct path_list changed
= {NULL
, 0, 0, 0};
80 git_config(git_default_config
);
82 newfd
= hold_lock_file_for_update(&lock_file
, get_index_file(), 1);
84 die("index file corrupt");
86 for (i
= 1; i
< argc
; i
++) {
87 const char *arg
= argv
[i
];
91 if (!strcmp(arg
, "--")) {
95 if (!strcmp(arg
, "-n")) {
99 if (!strcmp(arg
, "-f")) {
103 if (!strcmp(arg
, "-k")) {
107 usage(builtin_mv_usage
);
109 count
= argc
- i
- 1;
111 usage(builtin_mv_usage
);
113 source
= copy_pathspec(prefix
, argv
+ i
, count
, 0);
114 modes
= xcalloc(count
, sizeof(enum update_mode
));
115 dest_path
= copy_pathspec(prefix
, argv
+ argc
- 1, 1, 0);
117 if (!lstat(dest_path
[0], &st
) &&
118 S_ISDIR(st
.st_mode
)) {
119 dest_path
[0] = add_slash(dest_path
[0]);
120 destination
= copy_pathspec(dest_path
[0], argv
+ i
, count
, 1);
123 usage(builtin_mv_usage
);
124 destination
= dest_path
;
128 for (i
= 0; i
< count
; i
++) {
129 const char *src
= source
[i
], *dst
= destination
[i
];
130 int length
, src_is_dir
;
131 const char *bad
= NULL
;
134 printf("Checking rename of '%s' to '%s'\n", src
, dst
);
136 length
= strlen(src
);
137 if (lstat(src
, &st
) < 0)
139 else if (!strncmp(src
, dst
, length
) &&
140 (dst
[length
] == 0 || dst
[length
] == '/')) {
141 bad
= "can not move directory into itself";
142 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
143 && lstat(dst
, &st
) == 0)
144 bad
= "cannot move directory over file";
145 else if (src_is_dir
) {
148 modes
[i
] = WORKING_DIRECTORY
;
150 first
= cache_name_pos(src
, length
);
152 die ("Huh? %s/ is in index?", src
);
155 for (last
= first
; last
< active_nr
; last
++) {
156 const char *path
= active_cache
[last
]->name
;
157 if (strncmp(path
, src
, length
)
158 || path
[length
] != '/')
162 if (last
- first
< 1)
163 bad
= "source directory is empty";
167 if (last
- first
> 0) {
168 source
= realloc(source
,
169 (count
+ last
- first
)
171 destination
= realloc(destination
,
172 (count
+ last
- first
)
174 modes
= realloc(modes
,
175 (count
+ last
- first
)
176 * sizeof(enum update_mode
));
179 dst
= add_slash(dst
);
180 dst_len
= strlen(dst
) - 1;
182 for (j
= 0; j
< last
- first
; j
++) {
184 active_cache
[first
+ j
]->name
;
185 source
[count
+ j
] = path
;
186 destination
[count
+ j
] =
187 prefix_path(dst
, dst_len
,
189 modes
[count
+ j
] = INDEX
;
191 count
+= last
- first
;
193 } else if (lstat(dst
, &st
) == 0) {
194 bad
= "destination exists";
197 * only files can overwrite each other:
198 * check both source and destination
200 if (S_ISREG(st
.st_mode
)) {
201 fprintf(stderr
, "Warning: %s;"
202 " will overwrite!\n",
205 path_list_insert(dst
, &overwritten
);
207 bad
= "Cannot overwrite";
209 } else if (cache_name_pos(src
, length
) < 0)
210 bad
= "not under version control";
211 else if (path_list_has_path(&src_for_dst
, dst
))
212 bad
= "multiple sources for the same target";
214 path_list_insert(dst
, &src_for_dst
);
219 memmove(source
+ i
, source
+ i
+ 1,
220 (count
- i
) * sizeof(char *));
221 memmove(destination
+ i
,
223 (count
- i
) * sizeof(char *));
226 die ("%s, source=%s, destination=%s",
231 for (i
= 0; i
< count
; i
++) {
232 const char *src
= source
[i
], *dst
= destination
[i
];
233 enum update_mode mode
= modes
[i
];
234 if (show_only
|| verbose
)
235 printf("Renaming %s to %s\n", src
, dst
);
236 if (!show_only
&& mode
!= INDEX
&&
237 rename(src
, dst
) < 0 && !ignore_errors
)
238 die ("renaming %s failed: %s", src
, strerror(errno
));
240 if (mode
== WORKING_DIRECTORY
)
243 if (cache_name_pos(src
, strlen(src
)) >= 0) {
244 path_list_insert(src
, &deleted
);
246 /* destination can be a directory with 1 file inside */
247 if (path_list_has_path(&overwritten
, dst
))
248 path_list_insert(dst
, &changed
);
250 path_list_insert(dst
, &added
);
252 path_list_insert(dst
, &added
);
256 show_list("Changed : ", &changed
);
257 show_list("Adding : ", &added
);
258 show_list("Deleting : ", &deleted
);
260 for (i
= 0; i
< changed
.nr
; i
++) {
261 const char *path
= changed
.items
[i
].path
;
262 int i
= cache_name_pos(path
, strlen(path
));
263 struct cache_entry
*ce
= active_cache
[i
];
266 die ("Huh? Cache entry for %s unknown?", path
);
267 refresh_cache_entry(ce
, 0);
270 for (i
= 0; i
< added
.nr
; i
++) {
271 const char *path
= added
.items
[i
].path
;
272 add_file_to_index(path
, verbose
);
275 for (i
= 0; i
< deleted
.nr
; i
++) {
276 const char *path
= deleted
.items
[i
].path
;
277 remove_file_from_cache(path
);
280 if (active_cache_changed
) {
281 if (write_cache(newfd
, active_cache
, active_nr
) ||
283 commit_lock_file(&lock_file
))
284 die("Unable to write new index file");