2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
9 #include "cache-tree.h"
10 #include "string-list.h"
11 #include "parse-options.h"
12 #include "submodule.h"
14 static const char * const builtin_mv_usage
[] = {
15 N_("git mv [options] <source>... <destination>"),
19 #define DUP_BASENAME 1
20 #define KEEP_TRAILING_SLASH 2
22 static const char **internal_copy_pathspec(const char *prefix
,
23 const char **pathspec
,
24 int count
, unsigned flags
)
27 const char **result
= xmalloc((count
+ 1) * sizeof(const char *));
28 memcpy(result
, pathspec
, count
* sizeof(const char *));
30 for (i
= 0; i
< count
; i
++) {
31 int length
= strlen(result
[i
]);
33 while (!(flags
& KEEP_TRAILING_SLASH
) &&
34 to_copy
> 0 && is_dir_sep(result
[i
][to_copy
- 1]))
36 if (to_copy
!= length
|| flags
& DUP_BASENAME
) {
37 char *it
= xmemdupz(result
[i
], to_copy
);
38 if (flags
& DUP_BASENAME
) {
39 result
[i
] = xstrdup(basename(it
));
45 return get_pathspec(prefix
, result
);
48 static const char *add_slash(const char *path
)
50 int len
= strlen(path
);
51 if (path
[len
- 1] != '/') {
52 char *with_slash
= xmalloc(len
+ 2);
53 memcpy(with_slash
, path
, len
);
54 with_slash
[len
++] = '/';
61 static struct lock_file lock_file
;
62 #define SUBMODULE_WITH_GITDIR ((const char *)1)
64 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
66 int i
, newfd
, gitmodules_modified
= 0;
67 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
68 struct option builtin_mv_options
[] = {
69 OPT__VERBOSE(&verbose
, N_("be verbose")),
70 OPT__DRY_RUN(&show_only
, N_("dry run")),
71 OPT__FORCE(&force
, N_("force move/rename even if target exists")),
72 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
75 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
76 enum update_mode
{ BOTH
= 0, WORKING_DIRECTORY
, INDEX
} *modes
;
78 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
81 git_config(git_default_config
, NULL
);
83 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
86 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
88 newfd
= hold_locked_index(&lock_file
, 1);
90 die(_("index file corrupt"));
92 source
= internal_copy_pathspec(prefix
, argv
, argc
, 0);
93 modes
= xcalloc(argc
, sizeof(enum update_mode
));
95 * Keep trailing slash, needed to let
96 * "git mv file no-such-dir/" error out.
98 dest_path
= internal_copy_pathspec(prefix
, argv
+ argc
, 1,
100 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
102 if (dest_path
[0][0] == '\0')
103 /* special case: "." was normalized to "" */
104 destination
= internal_copy_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
105 else if (!lstat(dest_path
[0], &st
) &&
106 S_ISDIR(st
.st_mode
)) {
107 dest_path
[0] = add_slash(dest_path
[0]);
108 destination
= internal_copy_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
111 die("destination '%s' is not a directory", dest_path
[0]);
112 destination
= dest_path
;
116 for (i
= 0; i
< argc
; i
++) {
117 const char *src
= source
[i
], *dst
= destination
[i
];
118 int length
, src_is_dir
;
119 const char *bad
= NULL
;
122 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
124 length
= strlen(src
);
125 if (lstat(src
, &st
) < 0)
126 bad
= _("bad source");
127 else if (!strncmp(src
, dst
, length
) &&
128 (dst
[length
] == 0 || dst
[length
] == '/')) {
129 bad
= _("can not move directory into itself");
130 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
131 && lstat(dst
, &st
) == 0)
132 bad
= _("cannot move directory over file");
133 else if (src_is_dir
) {
134 int first
= cache_name_pos(src
, length
);
136 struct strbuf submodule_dotgit
= STRBUF_INIT
;
137 if (!S_ISGITLINK(active_cache
[first
]->ce_mode
))
138 die (_("Huh? Directory %s is in index and no submodule?"), src
);
139 if (!is_staging_gitmodules_ok())
140 die (_("Please, stage your changes to .gitmodules or stash them to proceed"));
141 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
142 submodule_gitfile
[i
] = read_gitfile(submodule_dotgit
.buf
);
143 if (submodule_gitfile
[i
])
144 submodule_gitfile
[i
] = xstrdup(submodule_gitfile
[i
]);
146 submodule_gitfile
[i
] = SUBMODULE_WITH_GITDIR
;
147 strbuf_release(&submodule_dotgit
);
149 const char *src_w_slash
= add_slash(src
);
150 int last
, len_w_slash
= length
+ 1;
152 modes
[i
] = WORKING_DIRECTORY
;
154 first
= cache_name_pos(src_w_slash
, len_w_slash
);
156 die (_("Huh? %.*s is in index?"),
157 len_w_slash
, src_w_slash
);
160 for (last
= first
; last
< active_nr
; last
++) {
161 const char *path
= active_cache
[last
]->name
;
162 if (strncmp(path
, src_w_slash
, len_w_slash
))
165 free((char *)src_w_slash
);
167 if (last
- first
< 1)
168 bad
= _("source directory is empty");
172 if (last
- first
> 0) {
173 source
= xrealloc(source
,
174 (argc
+ last
- first
)
176 destination
= xrealloc(destination
,
177 (argc
+ last
- first
)
179 modes
= xrealloc(modes
,
180 (argc
+ last
- first
)
181 * sizeof(enum update_mode
));
184 dst
= add_slash(dst
);
185 dst_len
= strlen(dst
);
187 for (j
= 0; j
< last
- first
; j
++) {
189 active_cache
[first
+ j
]->name
;
190 source
[argc
+ j
] = path
;
191 destination
[argc
+ j
] =
192 prefix_path(dst
, dst_len
,
194 modes
[argc
+ j
] = INDEX
;
196 argc
+= last
- first
;
199 } else if (cache_name_pos(src
, length
) < 0)
200 bad
= _("not under version control");
201 else if (lstat(dst
, &st
) == 0) {
202 bad
= _("destination exists");
205 * only files can overwrite each other:
206 * check both source and destination
208 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
210 warning(_("overwriting '%s'"), dst
);
213 bad
= _("Cannot overwrite");
215 } else if (string_list_has_string(&src_for_dst
, dst
))
216 bad
= _("multiple sources for the same target");
218 string_list_insert(&src_for_dst
, dst
);
223 memmove(source
+ i
, source
+ i
+ 1,
224 (argc
- i
) * sizeof(char *));
225 memmove(destination
+ i
,
227 (argc
- i
) * sizeof(char *));
231 die (_("%s, source=%s, destination=%s"),
236 for (i
= 0; i
< argc
; i
++) {
237 const char *src
= source
[i
], *dst
= destination
[i
];
238 enum update_mode mode
= modes
[i
];
240 if (show_only
|| verbose
)
241 printf(_("Renaming %s to %s\n"), src
, dst
);
242 if (!show_only
&& mode
!= INDEX
) {
243 if (rename(src
, dst
) < 0 && !ignore_errors
)
244 die_errno (_("renaming '%s' failed"), src
);
245 if (submodule_gitfile
[i
]) {
246 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
247 connect_work_tree_and_git_dir(dst
, submodule_gitfile
[i
]);
248 if (!update_path_in_gitmodules(src
, dst
))
249 gitmodules_modified
= 1;
253 if (mode
== WORKING_DIRECTORY
)
256 pos
= cache_name_pos(src
, strlen(src
));
259 rename_cache_entry_at(pos
, dst
);
262 if (gitmodules_modified
)
263 stage_updated_gitmodules();
265 if (active_cache_changed
) {
266 if (write_cache(newfd
, active_cache
, active_nr
) ||
267 commit_locked_index(&lock_file
))
268 die(_("Unable to write new index file"));