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 static void prepare_move_submodule(const char *src
, int first
,
65 const char **submodule_gitfile
)
67 struct strbuf submodule_dotgit
= STRBUF_INIT
;
68 if (!S_ISGITLINK(active_cache
[first
]->ce_mode
))
69 die(_("Directory %s is in index and no submodule?"), src
);
70 if (!is_staging_gitmodules_ok())
71 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
72 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
73 *submodule_gitfile
= read_gitfile(submodule_dotgit
.buf
);
74 if (*submodule_gitfile
)
75 *submodule_gitfile
= xstrdup(*submodule_gitfile
);
77 *submodule_gitfile
= SUBMODULE_WITH_GITDIR
;
78 strbuf_release(&submodule_dotgit
);
81 static int index_range_of_same_dir(const char *src
, int length
,
82 int *first_p
, int *last_p
)
84 const char *src_w_slash
= add_slash(src
);
85 int first
, last
, len_w_slash
= length
+ 1;
87 first
= cache_name_pos(src_w_slash
, len_w_slash
);
89 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
92 for (last
= first
; last
< active_nr
; last
++) {
93 const char *path
= active_cache
[last
]->name
;
94 if (strncmp(path
, src_w_slash
, len_w_slash
))
97 if (src_w_slash
!= src
)
98 free((char *)src_w_slash
);
104 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
106 int i
, gitmodules_modified
= 0;
107 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
108 struct option builtin_mv_options
[] = {
109 OPT__VERBOSE(&verbose
, N_("be verbose")),
110 OPT__DRY_RUN(&show_only
, N_("dry run")),
111 OPT__FORCE(&force
, N_("force move/rename even if target exists")),
112 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
115 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
116 enum update_mode
{ BOTH
= 0, WORKING_DIRECTORY
, INDEX
} *modes
;
118 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
121 git_config(git_default_config
, NULL
);
123 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
124 builtin_mv_usage
, 0);
126 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
128 hold_locked_index(&lock_file
, 1);
129 if (read_cache() < 0)
130 die(_("index file corrupt"));
132 source
= internal_copy_pathspec(prefix
, argv
, argc
, 0);
133 modes
= xcalloc(argc
, sizeof(enum update_mode
));
135 * Keep trailing slash, needed to let
136 * "git mv file no-such-dir/" error out.
138 dest_path
= internal_copy_pathspec(prefix
, argv
+ argc
, 1,
139 KEEP_TRAILING_SLASH
);
140 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
142 if (dest_path
[0][0] == '\0')
143 /* special case: "." was normalized to "" */
144 destination
= internal_copy_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
145 else if (!lstat(dest_path
[0], &st
) &&
146 S_ISDIR(st
.st_mode
)) {
147 dest_path
[0] = add_slash(dest_path
[0]);
148 destination
= internal_copy_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
151 die(_("destination '%s' is not a directory"), dest_path
[0]);
152 destination
= dest_path
;
156 for (i
= 0; i
< argc
; i
++) {
157 const char *src
= source
[i
], *dst
= destination
[i
];
158 int length
, src_is_dir
;
159 const char *bad
= NULL
;
162 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
164 length
= strlen(src
);
165 if (lstat(src
, &st
) < 0)
166 bad
= _("bad source");
167 else if (!strncmp(src
, dst
, length
) &&
168 (dst
[length
] == 0 || dst
[length
] == '/')) {
169 bad
= _("can not move directory into itself");
170 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
171 && lstat(dst
, &st
) == 0)
172 bad
= _("cannot move directory over file");
173 else if (src_is_dir
) {
174 int first
= cache_name_pos(src
, length
);
176 prepare_move_submodule(src
, first
,
177 submodule_gitfile
+ i
);
181 modes
[i
] = WORKING_DIRECTORY
;
182 index_range_of_same_dir(src
, length
, &first
, &last
);
183 if (last
- first
< 1)
184 bad
= _("source directory is empty");
188 n
= argc
+ last
- first
;
189 source
= xrealloc(source
, n
* sizeof(char *));
190 destination
= xrealloc(destination
, n
* sizeof(char *));
191 modes
= xrealloc(modes
, n
* sizeof(enum update_mode
));
193 xrealloc(submodule_gitfile
, n
* sizeof(char *));
195 dst
= add_slash(dst
);
196 dst_len
= strlen(dst
);
198 for (j
= 0; j
< last
- first
; j
++) {
200 active_cache
[first
+ j
]->name
;
201 source
[argc
+ j
] = path
;
202 destination
[argc
+ j
] =
203 prefix_path(dst
, dst_len
,
205 modes
[argc
+ j
] = INDEX
;
206 submodule_gitfile
[argc
+ j
] = NULL
;
208 argc
+= last
- first
;
211 } else if (cache_name_pos(src
, length
) < 0)
212 bad
= _("not under version control");
213 else if (lstat(dst
, &st
) == 0 &&
214 (!ignore_case
|| strcasecmp(src
, dst
))) {
215 bad
= _("destination exists");
218 * only files can overwrite each other:
219 * check both source and destination
221 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
223 warning(_("overwriting '%s'"), dst
);
226 bad
= _("Cannot overwrite");
228 } else if (string_list_has_string(&src_for_dst
, dst
))
229 bad
= _("multiple sources for the same target");
230 else if (is_dir_sep(dst
[strlen(dst
) - 1]))
231 bad
= _("destination directory does not exist");
233 string_list_insert(&src_for_dst
, dst
);
238 die (_("%s, source=%s, destination=%s"),
242 memmove(source
+ i
, source
+ i
+ 1,
244 memmove(destination
+ i
, destination
+ i
+ 1,
246 memmove(modes
+ i
, modes
+ i
+ 1,
247 n
* sizeof(enum update_mode
));
248 memmove(submodule_gitfile
+ i
, submodule_gitfile
+ i
+ 1,
254 for (i
= 0; i
< argc
; i
++) {
255 const char *src
= source
[i
], *dst
= destination
[i
];
256 enum update_mode mode
= modes
[i
];
258 if (show_only
|| verbose
)
259 printf(_("Renaming %s to %s\n"), src
, dst
);
260 if (!show_only
&& mode
!= INDEX
) {
261 if (rename(src
, dst
) < 0 && !ignore_errors
)
262 die_errno (_("renaming '%s' failed"), src
);
263 if (submodule_gitfile
[i
]) {
264 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
265 connect_work_tree_and_git_dir(dst
, submodule_gitfile
[i
]);
266 if (!update_path_in_gitmodules(src
, dst
))
267 gitmodules_modified
= 1;
271 if (mode
== WORKING_DIRECTORY
)
274 pos
= cache_name_pos(src
, strlen(src
));
277 rename_cache_entry_at(pos
, dst
);
280 if (gitmodules_modified
)
281 stage_updated_gitmodules();
283 if (active_cache_changed
) {
284 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
285 die(_("Unable to write new index file"));