Merge branch 'ss/builtin-cleanup'
[gitweb.git] / builtin / mv.c
index 2e0e61b6517d74edc8b7ef2e56c75927385e7c85..08fbc033e45bba4880c8de797ef5ac0dc0b6259d 100644 (file)
@@ -16,9 +16,12 @@ static const char * const builtin_mv_usage[] = {
        NULL
 };
 
+#define DUP_BASENAME 1
+#define KEEP_TRAILING_SLASH 2
+
 static const char **internal_copy_pathspec(const char *prefix,
                                           const char **pathspec,
-                                          int count, int base_name)
+                                          int count, unsigned flags)
 {
        int i;
        const char **result = xmalloc((count + 1) * sizeof(const char *));
@@ -27,11 +30,12 @@ static const char **internal_copy_pathspec(const char *prefix,
        for (i = 0; i < count; i++) {
                int length = strlen(result[i]);
                int to_copy = length;
-               while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
+               while (!(flags & KEEP_TRAILING_SLASH) &&
+                      to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
                        to_copy--;
-               if (to_copy != length || base_name) {
+               if (to_copy != length || flags & DUP_BASENAME) {
                        char *it = xmemdupz(result[i], to_copy);
-                       if (base_name) {
+                       if (flags & DUP_BASENAME) {
                                result[i] = xstrdup(basename(it));
                                free(it);
                        } else
@@ -87,16 +91,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
        source = internal_copy_pathspec(prefix, argv, argc, 0);
        modes = xcalloc(argc, sizeof(enum update_mode));
-       dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
+       /*
+        * Keep trailing slash, needed to let
+        * "git mv file no-such-dir/" error out.
+        */
+       dest_path = internal_copy_pathspec(prefix, argv + argc, 1,
+                                          KEEP_TRAILING_SLASH);
        submodule_gitfile = xcalloc(argc, sizeof(char *));
 
        if (dest_path[0][0] == '\0')
                /* special case: "." was normalized to "" */
-               destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
+               destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
        else if (!lstat(dest_path[0], &st) &&
                        S_ISDIR(st.st_mode)) {
                dest_path[0] = add_slash(dest_path[0]);
-               destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
+               destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
        } else {
                if (argc != 1)
                        die("destination '%s' is not a directory", dest_path[0]);