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 "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
);
32 result
[i
] = base_name
? strdup(basename(it
)) : it
;
35 return get_pathspec(prefix
, result
);
38 static const char *add_slash(const char *path
)
40 int len
= strlen(path
);
41 if (path
[len
- 1] != '/') {
42 char *with_slash
= xmalloc(len
+ 2);
43 memcpy(with_slash
, path
, len
);
44 with_slash
[len
++] = '/';
51 static struct lock_file lock_file
;
53 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
56 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
57 struct option builtin_mv_options
[] = {
58 OPT__VERBOSE(&verbose
, "be verbose"),
59 OPT__DRY_RUN(&show_only
, "dry run"),
60 OPT__FORCE(&force
, "force move/rename even if target exists"),
61 OPT_BOOLEAN('k', NULL
, &ignore_errors
, "skip move/rename errors"),
64 const char **source
, **destination
, **dest_path
;
65 enum update_mode
{ BOTH
= 0, WORKING_DIRECTORY
, INDEX
} *modes
;
67 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
69 git_config(git_default_config
, NULL
);
71 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
74 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
76 newfd
= hold_locked_index(&lock_file
, 1);
78 die(_("index file corrupt"));
80 source
= copy_pathspec(prefix
, argv
, argc
, 0);
81 modes
= xcalloc(argc
, sizeof(enum update_mode
));
82 dest_path
= copy_pathspec(prefix
, argv
+ argc
, 1, 0);
84 if (dest_path
[0][0] == '\0')
85 /* special case: "." was normalized to "" */
86 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
87 else if (!lstat(dest_path
[0], &st
) &&
88 S_ISDIR(st
.st_mode
)) {
89 dest_path
[0] = add_slash(dest_path
[0]);
90 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
93 die("destination '%s' is not a directory", dest_path
[0]);
94 destination
= dest_path
;
98 for (i
= 0; i
< argc
; i
++) {
99 const char *src
= source
[i
], *dst
= destination
[i
];
100 int length
, src_is_dir
;
101 const char *bad
= NULL
;
104 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
106 length
= strlen(src
);
107 if (lstat(src
, &st
) < 0)
108 bad
= _("bad source");
109 else if (!strncmp(src
, dst
, length
) &&
110 (dst
[length
] == 0 || dst
[length
] == '/')) {
111 bad
= _("can not move directory into itself");
112 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
113 && lstat(dst
, &st
) == 0)
114 bad
= _("cannot move directory over file");
115 else if (src_is_dir
) {
116 const char *src_w_slash
= add_slash(src
);
117 int len_w_slash
= length
+ 1;
120 modes
[i
] = WORKING_DIRECTORY
;
122 first
= cache_name_pos(src_w_slash
, len_w_slash
);
124 die (_("Huh? %.*s is in index?"),
125 len_w_slash
, src_w_slash
);
128 for (last
= first
; last
< active_nr
; last
++) {
129 const char *path
= active_cache
[last
]->name
;
130 if (strncmp(path
, src_w_slash
, len_w_slash
))
133 free((char *)src_w_slash
);
135 if (last
- first
< 1)
136 bad
= _("source directory is empty");
140 if (last
- first
> 0) {
141 source
= xrealloc(source
,
142 (argc
+ last
- first
)
144 destination
= xrealloc(destination
,
145 (argc
+ last
- first
)
147 modes
= xrealloc(modes
,
148 (argc
+ last
- first
)
149 * sizeof(enum update_mode
));
152 dst
= add_slash(dst
);
153 dst_len
= strlen(dst
);
155 for (j
= 0; j
< last
- first
; j
++) {
157 active_cache
[first
+ j
]->name
;
158 source
[argc
+ j
] = path
;
159 destination
[argc
+ j
] =
160 prefix_path(dst
, dst_len
,
162 modes
[argc
+ j
] = INDEX
;
164 argc
+= last
- first
;
166 } else if (cache_name_pos(src
, length
) < 0)
167 bad
= _("not under version control");
168 else if (lstat(dst
, &st
) == 0) {
169 bad
= _("destination exists");
172 * only files can overwrite each other:
173 * check both source and destination
175 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
176 warning(_("overwriting '%s'"), dst
);
179 bad
= _("Cannot overwrite");
181 } else if (string_list_has_string(&src_for_dst
, dst
))
182 bad
= _("multiple sources for the same target");
184 string_list_insert(&src_for_dst
, dst
);
189 memmove(source
+ i
, source
+ i
+ 1,
190 (argc
- i
) * sizeof(char *));
191 memmove(destination
+ i
,
193 (argc
- i
) * sizeof(char *));
197 die (_("%s, source=%s, destination=%s"),
202 for (i
= 0; i
< argc
; i
++) {
203 const char *src
= source
[i
], *dst
= destination
[i
];
204 enum update_mode mode
= modes
[i
];
206 if (show_only
|| verbose
)
207 printf(_("Renaming %s to %s\n"), src
, dst
);
208 if (!show_only
&& mode
!= INDEX
&&
209 rename(src
, dst
) < 0 && !ignore_errors
)
210 die_errno (_("renaming '%s' failed"), src
);
212 if (mode
== WORKING_DIRECTORY
)
215 pos
= cache_name_pos(src
, strlen(src
));
218 rename_cache_entry_at(pos
, dst
);
221 if (active_cache_changed
) {
222 if (write_cache(newfd
, active_cache
, active_nr
) ||
223 commit_locked_index(&lock_file
))
224 die(_("Unable to write new index file"));