2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
11 #include "cache-tree.h"
12 #include "path-list.h"
14 static const char builtin_mv_usage
[] =
15 "git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
17 static const char **copy_pathspec(const char *prefix
, const char **pathspec
,
18 int count
, int base_name
)
20 const char **result
= xmalloc((count
+ 1) * sizeof(const char *));
21 memcpy(result
, pathspec
, count
* sizeof(const char *));
25 for (i
= 0; i
< count
; i
++) {
26 const char *last_slash
= strrchr(result
[i
], '/');
28 result
[i
] = last_slash
+ 1;
31 return get_pathspec(prefix
, result
);
34 static void show_list(const char *label
, struct path_list
*list
)
39 for (i
= 0; i
< list
->nr
; i
++)
40 printf("%s%s", i
> 0 ? ", " : "", list
->items
[i
].path
);
45 static struct lock_file lock_file
;
47 int cmd_mv(int argc
, const char **argv
, char **envp
)
50 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0;
51 const char *prefix
= setup_git_directory();
52 const char **source
, **destination
, **dest_path
;
54 struct path_list overwritten
= {NULL
, 0, 0, 0};
55 struct path_list src_for_dst
= {NULL
, 0, 0, 0};
56 struct path_list added
= {NULL
, 0, 0, 0};
57 struct path_list deleted
= {NULL
, 0, 0, 0};
58 struct path_list changed
= {NULL
, 0, 0, 0};
60 git_config(git_default_config
);
62 newfd
= hold_lock_file_for_update(&lock_file
, get_index_file());
64 die("unable to create new index file");
67 die("index file corrupt");
69 for (i
= 1; i
< argc
; i
++) {
70 const char *arg
= argv
[i
];
74 if (!strcmp(arg
, "--")) {
78 if (!strcmp(arg
, "-n")) {
82 if (!strcmp(arg
, "-f")) {
86 if (!strcmp(arg
, "-k")) {
90 die(builtin_mv_usage
);
94 usage(builtin_mv_usage
);
96 source
= copy_pathspec(prefix
, argv
+ i
, count
, 0);
97 dest_path
= copy_pathspec(prefix
, argv
+ argc
- 1, 1, 0);
99 if (!lstat(dest_path
[0], &st
) && S_ISDIR(st
.st_mode
))
100 destination
= copy_pathspec(dest_path
[0], argv
+ i
, count
, 1);
103 usage(builtin_mv_usage
);
104 destination
= dest_path
;
108 for (i
= 0; i
< count
; i
++) {
109 const char *bad
= NULL
;
112 printf("Checking rename of '%s' to '%s'\n",
113 source
[i
], destination
[i
]);
115 if (lstat(source
[i
], &st
) < 0)
117 else if (lstat(destination
[i
], &st
) == 0) {
118 bad
= "destination exists";
121 * only files can overwrite each other:
122 * check both source and destination
124 if (S_ISREG(st
.st_mode
)) {
125 fprintf(stderr
, "Warning: %s;"
126 " will overwrite!\n",
129 path_list_insert(destination
[i
],
132 bad
= "Cannot overwrite";
137 !strncmp(destination
[i
], source
[i
], strlen(source
[i
])))
138 bad
= "can not move directory into itself";
140 if (!bad
&& cache_name_pos(source
[i
], strlen(source
[i
])) < 0)
141 bad
= "not under version control";
144 if (path_list_has_path(&src_for_dst
, destination
[i
]))
145 bad
= "multiple sources for the same target";
147 path_list_insert(destination
[i
], &src_for_dst
);
153 memmove(source
+ i
, source
+ i
+ 1,
154 (count
- i
) * sizeof(char *));
155 memmove(destination
+ i
,
157 (count
- i
) * sizeof(char *));
160 die ("Error: %s, source=%s, destination=%s",
161 bad
, source
[i
], destination
[i
]);
165 for (i
= 0; i
< count
; i
++) {
166 if (show_only
|| verbose
)
167 printf("Renaming %s to %s\n",
168 source
[i
], destination
[i
]);
170 rename(source
[i
], destination
[i
]) < 0 &&
172 die ("renaming %s failed: %s",
173 source
[i
], strerror(errno
));
175 if (cache_name_pos(source
[i
], strlen(source
[i
])) >= 0) {
176 path_list_insert(source
[i
], &deleted
);
178 /* destination can be a directory with 1 file inside */
179 if (path_list_has_path(&overwritten
, destination
[i
]))
180 path_list_insert(destination
[i
], &changed
);
182 path_list_insert(destination
[i
], &added
);
184 path_list_insert(destination
[i
], &added
);
188 show_list("Changed : ", &changed
);
189 show_list("Adding : ", &added
);
190 show_list("Deleting : ", &deleted
);
192 for (i
= 0; i
< changed
.nr
; i
++) {
193 const char *path
= changed
.items
[i
].path
;
194 int i
= cache_name_pos(path
, strlen(path
));
195 struct cache_entry
*ce
= active_cache
[i
];
198 die ("Huh? Cache entry for %s unknown?", path
);
199 refresh_cache_entry(ce
, 0);
202 for (i
= 0; i
< added
.nr
; i
++) {
203 const char *path
= added
.items
[i
].path
;
204 add_file_to_index(path
, verbose
);
207 for (i
= 0; i
< deleted
.nr
; i
++) {
208 const char *path
= deleted
.items
[i
].path
;
209 remove_file_from_cache(path
);
212 if (active_cache_changed
) {
213 if (write_cache(newfd
, active_cache
, active_nr
) ||
215 commit_lock_file(&lock_file
))
216 die("Unable to write new index file");