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
]);
27 if (length
> 0 && result
[i
][length
- 1] == '/') {
28 result
[i
] = xmemdupz(result
[i
], length
- 1);
31 const char *last_slash
= strrchr(result
[i
], '/');
33 result
[i
] = last_slash
+ 1;
36 return get_pathspec(prefix
, result
);
39 static const char *add_slash(const char *path
)
41 int len
= strlen(path
);
42 if (path
[len
- 1] != '/') {
43 char *with_slash
= xmalloc(len
+ 2);
44 memcpy(with_slash
, path
, len
);
45 with_slash
[len
++] = '/';
52 static struct lock_file lock_file
;
54 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
57 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
58 struct option builtin_mv_options
[] = {
59 OPT__DRY_RUN(&show_only
),
60 OPT_BOOLEAN('f', NULL
, &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
= {NULL
, 0, 0, 0};
69 git_config(git_default_config
, NULL
);
71 newfd
= hold_locked_index(&lock_file
, 1);
73 die("index file corrupt");
75 argc
= parse_options(argc
, argv
, builtin_mv_options
, builtin_mv_usage
, 0);
77 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
79 source
= copy_pathspec(prefix
, argv
, argc
, 0);
80 modes
= xcalloc(argc
, sizeof(enum update_mode
));
81 dest_path
= copy_pathspec(prefix
, argv
+ argc
, 1, 0);
83 if (dest_path
[0][0] == '\0')
84 /* special case: "." was normalized to "" */
85 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
86 else if (!lstat(dest_path
[0], &st
) &&
87 S_ISDIR(st
.st_mode
)) {
88 dest_path
[0] = add_slash(dest_path
[0]);
89 destination
= copy_pathspec(dest_path
[0], argv
, argc
, 1);
92 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
93 destination
= dest_path
;
97 for (i
= 0; i
< argc
; i
++) {
98 const char *src
= source
[i
], *dst
= destination
[i
];
99 int length
, src_is_dir
;
100 const char *bad
= NULL
;
103 printf("Checking rename of '%s' to '%s'\n", src
, dst
);
105 length
= strlen(src
);
106 if (lstat(src
, &st
) < 0)
108 else if (!strncmp(src
, dst
, length
) &&
109 (dst
[length
] == 0 || dst
[length
] == '/')) {
110 bad
= "can not move directory into itself";
111 } else if ((src_is_dir
= S_ISDIR(st
.st_mode
))
112 && lstat(dst
, &st
) == 0)
113 bad
= "cannot move directory over file";
114 else if (src_is_dir
) {
115 const char *src_w_slash
= add_slash(src
);
116 int len_w_slash
= length
+ 1;
119 modes
[i
] = WORKING_DIRECTORY
;
121 first
= cache_name_pos(src_w_slash
, len_w_slash
);
123 die ("Huh? %.*s is in index?",
124 len_w_slash
, src_w_slash
);
127 for (last
= first
; last
< active_nr
; last
++) {
128 const char *path
= active_cache
[last
]->name
;
129 if (strncmp(path
, src_w_slash
, len_w_slash
))
132 free((char *)src_w_slash
);
134 if (last
- first
< 1)
135 bad
= "source directory is empty";
139 if (last
- first
> 0) {
140 source
= xrealloc(source
,
141 (argc
+ last
- first
)
143 destination
= xrealloc(destination
,
144 (argc
+ last
- first
)
146 modes
= xrealloc(modes
,
147 (argc
+ last
- first
)
148 * sizeof(enum update_mode
));
151 dst
= add_slash(dst
);
152 dst_len
= strlen(dst
);
154 for (j
= 0; j
< last
- first
; j
++) {
156 active_cache
[first
+ j
]->name
;
157 source
[argc
+ j
] = path
;
158 destination
[argc
+ j
] =
159 prefix_path(dst
, dst_len
,
161 modes
[argc
+ j
] = INDEX
;
163 argc
+= last
- first
;
165 } else if (lstat(dst
, &st
) == 0) {
166 bad
= "destination exists";
169 * only files can overwrite each other:
170 * check both source and destination
172 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
173 fprintf(stderr
, "Warning: %s;"
174 " will overwrite!\n",
178 bad
= "Cannot overwrite";
180 } else if (cache_name_pos(src
, length
) < 0)
181 bad
= "not under version control";
182 else if (string_list_has_string(&src_for_dst
, dst
))
183 bad
= "multiple sources for the same target";
185 string_list_insert(dst
, &src_for_dst
);
190 memmove(source
+ i
, source
+ i
+ 1,
191 (argc
- i
) * sizeof(char *));
192 memmove(destination
+ i
,
194 (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 ("renaming %s failed: %s", src
, strerror(errno
));
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");