You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
5.9 KiB
228 lines
5.9 KiB
19 years ago
|
/*
|
||
|
* "git mv" builtin command
|
||
|
*
|
||
|
* Copyright (C) 2006 Johannes Schindelin
|
||
|
*/
|
||
|
#include "cache.h"
|
||
|
#include "builtin.h"
|
||
|
#include "dir.h"
|
||
|
#include "cache-tree.h"
|
||
17 years ago
|
#include "string-list.h"
|
||
17 years ago
|
#include "parse-options.h"
|
||
19 years ago
|
|
||
17 years ago
|
static const char * const builtin_mv_usage[] = {
|
||
17 years ago
|
"git mv [options] <source>... <destination>",
|
||
17 years ago
|
NULL
|
||
|
};
|
||
19 years ago
|
|
||
|
static const char **copy_pathspec(const char *prefix, const char **pathspec,
|
||
|
int count, int base_name)
|
||
|
{
|
||
19 years ago
|
int i;
|
||
19 years ago
|
const char **result = xmalloc((count + 1) * sizeof(const char *));
|
||
|
memcpy(result, pathspec, count * sizeof(const char *));
|
||
|
result[count] = NULL;
|
||
19 years ago
|
for (i = 0; i < count; i++) {
|
||
|
int length = strlen(result[i]);
|
||
15 years ago
|
int to_copy = length;
|
||
|
while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
|
||
|
to_copy--;
|
||
|
if (to_copy != length || base_name) {
|
||
|
char *it = xmemdupz(result[i], to_copy);
|
||
|
result[i] = base_name ? strdup(basename(it)) : it;
|
||
|
}
|
||
19 years ago
|
}
|
||
17 years ago
|
return get_pathspec(prefix, result);
|
||
19 years ago
|
}
|
||
|
|
||
19 years ago
|
static const char *add_slash(const char *path)
|
||
|
{
|
||
|
int len = strlen(path);
|
||
|
if (path[len - 1] != '/') {
|
||
|
char *with_slash = xmalloc(len + 2);
|
||
|
memcpy(with_slash, path, len);
|
||
19 years ago
|
with_slash[len++] = '/';
|
||
|
with_slash[len] = 0;
|
||
19 years ago
|
return with_slash;
|
||
|
}
|
||
|
return path;
|
||
|
}
|
||
|
|
||
19 years ago
|
static struct lock_file lock_file;
|
||
|
|
||
19 years ago
|
int cmd_mv(int argc, const char **argv, const char *prefix)
|
||
19 years ago
|
{
|
||
17 years ago
|
int i, newfd;
|
||
19 years ago
|
int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
|
||
17 years ago
|
struct option builtin_mv_options[] = {
|
||
|
OPT__DRY_RUN(&show_only),
|
||
16 years ago
|
OPT_BOOLEAN('f', "force", &force, "force move/rename even if target exists"),
|
||
17 years ago
|
OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
|
||
|
OPT_END(),
|
||
|
};
|
||
19 years ago
|
const char **source, **destination, **dest_path;
|
||
19 years ago
|
enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
|
||
19 years ago
|
struct stat st;
|
||
17 years ago
|
struct string_list src_for_dst = {NULL, 0, 0, 0};
|
||
19 years ago
|
|
||
17 years ago
|
git_config(git_default_config, NULL);
|
||
19 years ago
|
|
||
16 years ago
|
argc = parse_options(argc, argv, prefix, builtin_mv_options,
|
||
|
builtin_mv_usage, 0);
|
||
17 years ago
|
if (--argc < 1)
|
||
|
usage_with_options(builtin_mv_usage, builtin_mv_options);
|
||
19 years ago
|
|
||
15 years ago
|
newfd = hold_locked_index(&lock_file, 1);
|
||
|
if (read_cache() < 0)
|
||
|
die("index file corrupt");
|
||
|
|
||
17 years ago
|
source = copy_pathspec(prefix, argv, argc, 0);
|
||
|
modes = xcalloc(argc, sizeof(enum update_mode));
|
||
|
dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
|
||
19 years ago
|
|
||
19 years ago
|
if (dest_path[0][0] == '\0')
|
||
|
/* special case: "." was normalized to "" */
|
||
17 years ago
|
destination = copy_pathspec(dest_path[0], argv, argc, 1);
|
||
19 years ago
|
else if (!lstat(dest_path[0], &st) &&
|
||
19 years ago
|
S_ISDIR(st.st_mode)) {
|
||
|
dest_path[0] = add_slash(dest_path[0]);
|
||
17 years ago
|
destination = copy_pathspec(dest_path[0], argv, argc, 1);
|
||
19 years ago
|
} else {
|
||
17 years ago
|
if (argc != 1)
|
||
|
usage_with_options(builtin_mv_usage, builtin_mv_options);
|
||
19 years ago
|
destination = dest_path;
|
||
|
}
|
||
|
|
||
|
/* Checking */
|
||
17 years ago
|
for (i = 0; i < argc; i++) {
|
||
19 years ago
|
const char *src = source[i], *dst = destination[i];
|
||
|
int length, src_is_dir;
|
||
19 years ago
|
const char *bad = NULL;
|
||
|
|
||
|
if (show_only)
|
||
19 years ago
|
printf("Checking rename of '%s' to '%s'\n", src, dst);
|
||
19 years ago
|
|
||
19 years ago
|
length = strlen(src);
|
||
|
if (lstat(src, &st) < 0)
|
||
19 years ago
|
bad = "bad source";
|
||
19 years ago
|
else if (!strncmp(src, dst, length) &&
|
||
|
(dst[length] == 0 || dst[length] == '/')) {
|
||
19 years ago
|
bad = "can not move directory into itself";
|
||
19 years ago
|
} else if ((src_is_dir = S_ISDIR(st.st_mode))
|
||
|
&& lstat(dst, &st) == 0)
|
||
|
bad = "cannot move directory over file";
|
||
|
else if (src_is_dir) {
|
||
18 years ago
|
const char *src_w_slash = add_slash(src);
|
||
|
int len_w_slash = length + 1;
|
||
19 years ago
|
int first, last;
|
||
19 years ago
|
|
||
|
modes[i] = WORKING_DIRECTORY;
|
||
|
|
||
18 years ago
|
first = cache_name_pos(src_w_slash, len_w_slash);
|
||
19 years ago
|
if (first >= 0)
|
||
18 years ago
|
die ("Huh? %.*s is in index?",
|
||
|
len_w_slash, src_w_slash);
|
||
19 years ago
|
|
||
|
first = -1 - first;
|
||
|
for (last = first; last < active_nr; last++) {
|
||
|
const char *path = active_cache[last]->name;
|
||
18 years ago
|
if (strncmp(path, src_w_slash, len_w_slash))
|
||
19 years ago
|
break;
|
||
|
}
|
||
18 years ago
|
free((char *)src_w_slash);
|
||
19 years ago
|
|
||
|
if (last - first < 1)
|
||
|
bad = "source directory is empty";
|
||
19 years ago
|
else {
|
||
|
int j, dst_len;
|
||
19 years ago
|
|
||
|
if (last - first > 0) {
|
||
19 years ago
|
source = xrealloc(source,
|
||
17 years ago
|
(argc + last - first)
|
||
19 years ago
|
* sizeof(char *));
|
||
19 years ago
|
destination = xrealloc(destination,
|
||
17 years ago
|
(argc + last - first)
|
||
19 years ago
|
* sizeof(char *));
|
||
19 years ago
|
modes = xrealloc(modes,
|
||
17 years ago
|
(argc + last - first)
|
||
19 years ago
|
* sizeof(enum update_mode));
|
||
|
}
|
||
|
|
||
19 years ago
|
dst = add_slash(dst);
|
||
17 years ago
|
dst_len = strlen(dst);
|
||
19 years ago
|
|
||
|
for (j = 0; j < last - first; j++) {
|
||
|
const char *path =
|
||
|
active_cache[first + j]->name;
|
||
17 years ago
|
source[argc + j] = path;
|
||
|
destination[argc + j] =
|
||
19 years ago
|
prefix_path(dst, dst_len,
|
||
17 years ago
|
path + length + 1);
|
||
17 years ago
|
modes[argc + j] = INDEX;
|
||
19 years ago
|
}
|
||
17 years ago
|
argc += last - first;
|
||
19 years ago
|
}
|
||
16 years ago
|
} else if (cache_name_pos(src, length) < 0)
|
||
|
bad = "not under version control";
|
||
|
else if (lstat(dst, &st) == 0) {
|
||
19 years ago
|
bad = "destination exists";
|
||
|
if (force) {
|
||
|
/*
|
||
|
* only files can overwrite each other:
|
||
|
* check both source and destination
|
||
|
*/
|
||
17 years ago
|
if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
|
||
15 years ago
|
warning("%s; will overwrite!", bad);
|
||
19 years ago
|
bad = NULL;
|
||
|
} else
|
||
|
bad = "Cannot overwrite";
|
||
|
}
|
||
16 years ago
|
} else if (string_list_has_string(&src_for_dst, dst))
|
||
19 years ago
|
bad = "multiple sources for the same target";
|
||
|
else
|
||
17 years ago
|
string_list_insert(dst, &src_for_dst);
|
||
19 years ago
|
|
||
|
if (bad) {
|
||
|
if (ignore_errors) {
|
||
17 years ago
|
if (--argc > 0) {
|
||
19 years ago
|
memmove(source + i, source + i + 1,
|
||
17 years ago
|
(argc - i) * sizeof(char *));
|
||
19 years ago
|
memmove(destination + i,
|
||
|
destination + i + 1,
|
||
17 years ago
|
(argc - i) * sizeof(char *));
|
||
16 years ago
|
i--;
|
||
19 years ago
|
}
|
||
|
} else
|
||
19 years ago
|
die ("%s, source=%s, destination=%s",
|
||
19 years ago
|
bad, src, dst);
|
||
19 years ago
|
}
|
||
|
}
|
||
|
|
||
17 years ago
|
for (i = 0; i < argc; i++) {
|
||
19 years ago
|
const char *src = source[i], *dst = destination[i];
|
||
|
enum update_mode mode = modes[i];
|
||
17 years ago
|
int pos;
|
||
19 years ago
|
if (show_only || verbose)
|
||
19 years ago
|
printf("Renaming %s to %s\n", src, dst);
|
||
|
if (!show_only && mode != INDEX &&
|
||
|
rename(src, dst) < 0 && !ignore_errors)
|
||
16 years ago
|
die_errno ("renaming '%s' failed", src);
|
||
19 years ago
|
|
||
|
if (mode == WORKING_DIRECTORY)
|
||
19 years ago
|
continue;
|
||
|
|
||
17 years ago
|
pos = cache_name_pos(src, strlen(src));
|
||
|
assert(pos >= 0);
|
||
|
if (!show_only)
|
||
|
rename_cache_entry_at(pos, dst);
|
||
19 years ago
|
}
|
||
|
|
||
17 years ago
|
if (active_cache_changed) {
|
||
|
if (write_cache(newfd, active_cache, active_nr) ||
|
||
|
commit_locked_index(&lock_file))
|
||
|
die("Unable to write new index file");
|
||
19 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|