11
#include "environment.h"
14
#include "object-file.h"
18
#include "string-list.h"
19
#include "parse-options.h"
20
#include "read-cache-ll.h"
21
#include "repository.h"
27
static const char * const builtin_mv_usage[] = {
28
N_("git mv [<options>] <source>... <destination>"),
33
WORKING_DIRECTORY = (1 << 1),
36
SKIP_WORKTREE_DIR = (1 << 4),
40
#define KEEP_TRAILING_SLASH 2
42
static void internal_prefix_pathspec(struct strvec *out,
44
const char **pathspec,
45
int count, unsigned flags)
47
int prefixlen = prefix ? strlen(prefix) : 0;
50
for (int i = 0; i < count; i++) {
51
size_t length = strlen(pathspec[i]);
52
size_t to_copy = length;
53
const char *maybe_basename;
54
char *trimmed, *prefixed_path;
56
while (!(flags & KEEP_TRAILING_SLASH) &&
57
to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
60
trimmed = xmemdupz(pathspec[i], to_copy);
61
maybe_basename = (flags & DUP_BASENAME) ? basename(trimmed) : trimmed;
62
prefixed_path = prefix_path(prefix, prefixlen, maybe_basename);
63
strvec_push(out, prefixed_path);
70
static char *add_slash(const char *path)
72
size_t len = strlen(path);
73
if (len && path[len - 1] != '/') {
74
char *with_slash = xmalloc(st_add(len, 2));
75
memcpy(with_slash, path, len);
76
with_slash[len++] = '/';
83
#define SUBMODULE_WITH_GITDIR ((const char *)1)
85
static const char *submodule_gitfile_path(const char *src, int first)
87
struct strbuf submodule_dotgit = STRBUF_INIT;
90
if (!S_ISGITLINK(the_repository->index->cache[first]->ce_mode))
91
die(_("Directory %s is in index and no submodule?"), src);
92
if (!is_staging_gitmodules_ok(the_repository->index))
93
die(_("Please stage your changes to .gitmodules or stash them to proceed"));
95
strbuf_addf(&submodule_dotgit, "%s/.git", src);
97
path = read_gitfile(submodule_dotgit.buf);
98
strbuf_release(&submodule_dotgit);
101
return SUBMODULE_WITH_GITDIR;
104
static int index_range_of_same_dir(const char *src, int length,
105
int *first_p, int *last_p)
107
char *src_w_slash = add_slash(src);
108
int first, last, len_w_slash = length + 1;
110
first = index_name_pos(the_repository->index, src_w_slash, len_w_slash);
112
die(_("%.*s is in index"), len_w_slash, src_w_slash);
115
for (last = first; last < the_repository->index->cache_nr; last++) {
116
const char *path = the_repository->index->cache[last]->name;
117
if (strncmp(path, src_w_slash, len_w_slash))
134
static int empty_dir_has_sparse_contents(const char *name)
137
char *with_slash = add_slash(name);
138
int length = strlen(with_slash);
140
int pos = index_name_pos(the_repository->index, with_slash, length);
141
const struct cache_entry *ce;
145
if (pos >= the_repository->index->cache_nr)
147
ce = the_repository->index->cache[pos];
148
if (strncmp(with_slash, ce->name, length))
150
if (ce_skip_worktree(ce))
159
static void remove_empty_src_dirs(const char **src_dir, size_t src_dir_nr)
162
struct strbuf a_src_dir = STRBUF_INIT;
164
for (i = 0; i < src_dir_nr; i++) {
166
strbuf_addstr(&a_src_dir, src_dir[i]);
171
if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
172
&dummy, &dummy) < 1) {
173
remove_dir_recursively(&a_src_dir, 0);
175
strbuf_reset(&a_src_dir);
178
strbuf_release(&a_src_dir);
181
int cmd_mv(int argc, const char **argv, const char *prefix)
183
int i, flags, gitmodules_modified = 0;
184
int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0;
185
struct option builtin_mv_options[] = {
186
OPT__VERBOSE(&verbose, N_("be verbose")),
187
OPT__DRY_RUN(&show_only, N_("dry run")),
188
OPT__FORCE(&force, N_("force move/rename even if target exists"),
189
PARSE_OPT_NOCOMPLETE),
190
OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
191
OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
194
struct strvec sources = STRVEC_INIT;
195
struct strvec dest_paths = STRVEC_INIT;
196
struct strvec destinations = STRVEC_INIT;
197
struct strvec submodule_gitfiles_to_free = STRVEC_INIT;
198
const char **submodule_gitfiles;
199
char *dst_w_slash = NULL;
200
struct strvec src_dir = STRVEC_INIT;
201
enum update_mode *modes, dst_mode = 0;
202
struct stat st, dest_st;
203
struct string_list src_for_dst = STRING_LIST_INIT_DUP;
204
struct lock_file lock_file = LOCK_INIT;
205
struct cache_entry *ce;
206
struct string_list only_match_skip_worktree = STRING_LIST_INIT_DUP;
207
struct string_list dirty_paths = STRING_LIST_INIT_DUP;
210
git_config(git_default_config, NULL);
212
argc = parse_options(argc, argv, prefix, builtin_mv_options,
213
builtin_mv_usage, 0);
215
usage_with_options(builtin_mv_usage, builtin_mv_options);
217
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
218
if (repo_read_index(the_repository) < 0)
219
die(_("index file corrupt"));
221
internal_prefix_pathspec(&sources, prefix, argv, argc, 0);
222
CALLOC_ARRAY(modes, argc);
229
flags = KEEP_TRAILING_SLASH;
230
if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
232
internal_prefix_pathspec(&dest_paths, prefix, argv + argc, 1, flags);
233
dst_w_slash = add_slash(dest_paths.v[0]);
234
submodule_gitfiles = xcalloc(argc, sizeof(char *));
236
if (dest_paths.v[0][0] == '\0')
238
internal_prefix_pathspec(&destinations, dest_paths.v[0], argv, argc, DUP_BASENAME);
239
else if (!lstat(dest_paths.v[0], &st) && S_ISDIR(st.st_mode)) {
240
internal_prefix_pathspec(&destinations, dst_w_slash, argv, argc, DUP_BASENAME);
241
} else if (!path_in_sparse_checkout(dst_w_slash, the_repository->index) &&
242
empty_dir_has_sparse_contents(dst_w_slash)) {
243
internal_prefix_pathspec(&destinations, dst_w_slash, argv, argc, DUP_BASENAME);
244
dst_mode = SKIP_WORKTREE_DIR;
245
} else if (argc != 1) {
246
die(_("destination '%s' is not a directory"), dest_paths.v[0]);
248
strvec_pushv(&destinations, dest_paths.v);
258
if (!path_in_cone_mode_sparse_checkout(destinations.v[0], the_repository->index))
263
for (i = 0; i < argc; i++) {
264
const char *src = sources.v[i], *dst = destinations.v[i];
266
const char *bad = NULL;
270
printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
272
length = strlen(src);
273
if (lstat(src, &st) < 0) {
275
const struct cache_entry *ce;
277
pos = index_name_pos(the_repository->index, src, length);
279
char *src_w_slash = add_slash(src);
280
if (!path_in_sparse_checkout(src_w_slash, the_repository->index) &&
281
empty_dir_has_sparse_contents(src)) {
283
modes[i] |= SKIP_WORKTREE_DIR;
288
if (!(modes[i] & SPARSE))
289
bad = _("bad source");
292
ce = the_repository->index->cache[pos];
293
if (!ce_skip_worktree(ce)) {
294
bad = _("bad source");
297
if (!ignore_sparse) {
298
string_list_append(&only_match_skip_worktree, src);
302
if (index_name_pos(the_repository->index, dst, strlen(dst)) < 0) {
307
bad = _("destination exists");
313
if (!strncmp(src, dst, length) &&
314
(dst[length] == 0 || dst[length] == '/')) {
315
bad = _("can not move directory into itself");
318
if (S_ISDIR(st.st_mode)
319
&& lstat(dst, &dest_st) == 0) {
320
bad = _("destination already exists");
325
if (S_ISDIR(st.st_mode)) {
326
char *dst_with_slash;
327
size_t dst_with_slash_len;
329
int first = index_name_pos(the_repository->index, src, length), last;
332
const char *path = submodule_gitfile_path(src, first);
333
if (path != SUBMODULE_WITH_GITDIR)
334
path = strvec_push(&submodule_gitfiles_to_free, path);
335
submodule_gitfiles[i] = path;
337
} else if (index_range_of_same_dir(src, length,
338
&first, &last) < 1) {
339
bad = _("source directory is empty");
344
modes[i] |= WORKING_DIRECTORY;
346
strvec_push(&src_dir, src);
348
n = argc + last - first;
349
REALLOC_ARRAY(modes, n);
350
REALLOC_ARRAY(submodule_gitfiles, n);
352
dst_with_slash = add_slash(dst);
353
dst_with_slash_len = strlen(dst_with_slash);
355
for (j = 0; j < last - first; j++) {
356
const struct cache_entry *ce = the_repository->index->cache[first + j];
357
const char *path = ce->name;
358
char *prefixed_path = prefix_path(dst_with_slash, dst_with_slash_len, path + length + 1);
360
strvec_push(&sources, path);
361
strvec_push(&destinations, prefixed_path);
363
memset(modes + argc + j, 0, sizeof(enum update_mode));
364
modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
365
submodule_gitfiles[argc + j] = NULL;
370
free(dst_with_slash);
371
argc += last - first;
374
if (!(ce = index_file_exists(the_repository->index, src, length, 0))) {
375
bad = _("not under version control");
379
bad = _("conflicted");
382
if (lstat(dst, &st) == 0 &&
383
(!ignore_case || strcasecmp(src, dst))) {
384
bad = _("destination exists");
390
if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
392
warning(_("overwriting '%s'"), dst);
395
bad = _("Cannot overwrite");
399
if (string_list_has_string(&src_for_dst, dst)) {
400
bad = _("multiple sources for the same target");
403
if (is_dir_sep(dst[strlen(dst) - 1])) {
404
bad = _("destination directory does not exist");
409
(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
410
index_entry_exists(the_repository->index, dst, strlen(dst))) {
411
bad = _("destination exists in the index");
414
warning(_("overwriting '%s'"), dst);
426
if (!ignore_sparse &&
427
!path_in_sparse_checkout(src, the_repository->index)) {
428
string_list_append(&only_match_skip_worktree, src);
431
if (!ignore_sparse &&
432
!path_in_sparse_checkout(dst, the_repository->index)) {
433
string_list_append(&only_match_skip_worktree, dst);
440
string_list_insert(&src_for_dst, dst);
446
die(_("%s, source=%s, destination=%s"),
451
strvec_remove(&sources, i);
452
strvec_remove(&destinations, i);
453
MOVE_ARRAY(modes + i, modes + i + 1, n);
454
MOVE_ARRAY(submodule_gitfiles + i,
455
submodule_gitfiles + i + 1, n);
460
if (only_match_skip_worktree.nr) {
461
advise_on_updating_sparse_paths(&only_match_skip_worktree);
462
if (!ignore_errors) {
468
for (i = 0; i < argc; i++) {
469
const char *src = sources.v[i], *dst = destinations.v[i];
470
enum update_mode mode = modes[i];
472
int sparse_and_dirty = 0;
473
struct checkout state = CHECKOUT_INIT;
474
state.istate = the_repository->index;
478
if (show_only || verbose)
479
printf(_("Renaming %s to %s\n"), src, dst);
482
if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
483
!(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
484
rename(src, dst) < 0) {
487
die_errno(_("renaming '%s' failed"), src);
489
if (submodule_gitfiles[i]) {
490
if (!update_path_in_gitmodules(src, dst))
491
gitmodules_modified = 1;
492
if (submodule_gitfiles[i] != SUBMODULE_WITH_GITDIR)
493
connect_work_tree_and_git_dir(dst,
494
submodule_gitfiles[i],
498
if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
501
pos = index_name_pos(the_repository->index, src, strlen(src));
503
if (!(mode & SPARSE) && !lstat(src, &st))
504
sparse_and_dirty = ie_modified(the_repository->index,
505
the_repository->index->cache[pos],
508
rename_index_entry_at(the_repository->index, pos, dst);
511
core_apply_sparse_checkout &&
512
core_sparse_checkout_cone) {
519
if ((mode & SPARSE) &&
520
path_in_sparse_checkout(dst, the_repository->index)) {
522
int dst_pos = index_name_pos(the_repository->index, dst,
524
struct cache_entry *dst_ce = the_repository->index->cache[dst_pos];
526
dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
528
if (checkout_entry(dst_ce, &state, NULL, NULL))
529
die(_("cannot checkout %s"), dst_ce->name);
530
} else if ((dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
532
!path_in_sparse_checkout(dst, the_repository->index)) {
534
int dst_pos = index_name_pos(the_repository->index, dst,
536
struct cache_entry *dst_ce = the_repository->index->cache[dst_pos];
541
if (!sparse_and_dirty) {
542
dst_ce->ce_flags |= CE_SKIP_WORKTREE;
550
char *dst_dup = xstrdup(dst);
551
string_list_append(&dirty_paths, dst);
552
safe_create_leading_directories(dst_dup);
553
FREE_AND_NULL(dst_dup);
560
remove_empty_src_dirs(src_dir.v, src_dir.nr);
563
advise_on_moving_dirty_path(&dirty_paths);
565
if (gitmodules_modified)
566
stage_updated_gitmodules(the_repository->index);
568
if (write_locked_index(the_repository->index, &lock_file,
569
COMMIT_LOCK | SKIP_IF_UNCHANGED))
570
die(_("Unable to write new index file"));
575
strvec_clear(&src_dir);
577
string_list_clear(&src_for_dst, 0);
578
string_list_clear(&dirty_paths, 0);
579
string_list_clear(&only_match_skip_worktree, 0);
580
strvec_clear(&sources);
581
strvec_clear(&dest_paths);
582
strvec_clear(&destinations);
583
strvec_clear(&submodule_gitfiles_to_free);
584
free(submodule_gitfiles);