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"
13 static const char * const builtin_mv_usage
[] = {
14 N_("git mv [options] <source>... <destination>"),
18 static const char **copy_pathspec(const char *prefix
, const char **pathspec
,
19 int count
, int base_name
)
22 const char **result
= xmalloc((count
+ 1) * sizeof(const char *));
23 memcpy(result
, pathspec
, count
* sizeof(const char *));
25 for (i
= 0; i
< count
; i
++) {
26 int length
= strlen(result
[i
]);
28 while (to_copy
> 0 && is_dir_sep(result
[i
][to_copy
- 1]))
30 if (to_copy
!= length
|| base_name
) {
31 char *it
= xmemdupz(result
[i
], to_copy
);
33 result
[i
] = xstrdup(basename(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
++] = '/';
55 static struct lock_file lock_file
;
57 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
60 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
61 struct option builtin_mv_options
[] = {
62 OPT__VERBOSE(&verbose
, N_("be verbose")),
63 OPT__DRY_RUN(&show_only
, N_("dry run")),
64 OPT__FORCE(&force
, N_("force move/rename even if target exists")),
65 OPT_BOOLEAN('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
68 const char **source
, **destination
, **dest_path
;
69 enum update_mode
{ BOTH
= 0, WORKING_DIRECTORY
, INDEX
} *modes
;
71 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
73 git_config(git_default_config
, NULL
);
75 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
78 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
80 newfd
= hold_locked_index(&lock_file
, 1);
82 die(_("index file corrupt"));
84 source
= copy_pathspec(prefix
, argv
, argc
, 0);
85 modes
= xcalloc(argc
, sizeof(enum update_mode
));
86 dest_path
= copy_pathspec(prefix
, argv
+ argc
, 1, 0);
88 if (dest_path
[0][0] == '\0')
89 /* special case: "." was normalized to "" */
90 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
91 else if (!lstat(dest_path
[0], &st
) &&
92 S_ISDIR(st
.st_mode
)) {
93 dest_path
[0] = add_slash(dest_path
[0]);
94 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
97 die("destination '%s' is not a directory", dest_path
[0]);
98 destination
= dest_path
;
102 for (i
= 0; i
< argc
; i
++) {
103 const char *src
= source
[i
], *dst
= destination
[i
];
104 int length
, src_is_dir
;
105 const char *bad
= NULL
;
108 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
110 length
= strlen(src
);
111 if (lstat(src
, &st
) < 0)
112 bad
= _("bad source");
113 else if (!strncmp(src
, dst
, length
) &&
114 (dst
[length
] == 0 || dst
[length
] == '/')) {
115 bad
= _("can not move directory into itself");
116 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
117 && lstat(dst
, &st
) == 0)
118 bad
= _("cannot move directory over file");
119 else if (src_is_dir
) {
120 const char *src_w_slash
= add_slash(src
);
121 int len_w_slash
= length
+ 1;
124 modes
[i
] = WORKING_DIRECTORY
;
126 first
= cache_name_pos(src_w_slash
, len_w_slash
);
128 die (_("Huh? %.*s is in index?"),
129 len_w_slash
, src_w_slash
);
132 for (last
= first
; last
< active_nr
; last
++) {
133 const char *path
= active_cache
[last
]->name
;
134 if (strncmp(path
, src_w_slash
, len_w_slash
))
137 free((char *)src_w_slash
);
139 if (last
- first
< 1)
140 bad
= _("source directory is empty");
144 if (last
- first
> 0) {
145 source
= xrealloc(source
,
146 (argc
+ last
- first
)
148 destination
= xrealloc(destination
,
149 (argc
+ last
- first
)
151 modes
= xrealloc(modes
,
152 (argc
+ last
- first
)
153 * sizeof(enum update_mode
));
156 dst
= add_slash(dst
);
157 dst_len
= strlen(dst
);
159 for (j
= 0; j
< last
- first
; j
++) {
161 active_cache
[first
+ j
]->name
;
162 source
[argc
+ j
] = path
;
163 destination
[argc
+ j
] =
164 prefix_path(dst
, dst_len
,
166 modes
[argc
+ j
] = INDEX
;
168 argc
+= last
- first
;
170 } else if (cache_name_pos(src
, length
) < 0)
171 bad
= _("not under version control");
172 else if (lstat(dst
, &st
) == 0) {
173 bad
= _("destination exists");
176 * only files can overwrite each other:
177 * check both source and destination
179 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
181 warning(_("overwriting '%s'"), dst
);
184 bad
= _("Cannot overwrite");
186 } else if (string_list_has_string(&src_for_dst
, dst
))
187 bad
= _("multiple sources for the same target");
189 string_list_insert(&src_for_dst
, dst
);
194 memmove(source
+ i
, source
+ i
+ 1,
195 (argc
- i
) * sizeof(char *));
196 memmove(destination
+ i
,
198 (argc
- i
) * sizeof(char *));
202 die (_("%s, source=%s, destination=%s"),
207 for (i
= 0; i
< argc
; i
++) {
208 const char *src
= source
[i
], *dst
= destination
[i
];
209 enum update_mode mode
= modes
[i
];
211 if (show_only
|| verbose
)
212 printf(_("Renaming %s to %s\n"), src
, dst
);
213 if (!show_only
&& mode
!= INDEX
&&
214 rename(src
, dst
) < 0 && !ignore_errors
)
215 die_errno (_("renaming '%s' failed"), src
);
217 if (mode
== WORKING_DIRECTORY
)
220 pos
= cache_name_pos(src
, strlen(src
));
223 rename_cache_entry_at(pos
, dst
);
226 if (active_cache_changed
) {
227 if (write_cache(newfd
, active_cache
, active_nr
) ||
228 commit_locked_index(&lock_file
))
229 die(_("Unable to write new index file"));