Code

gitweb: Remove invalid comment in format_diff_line
[git.git] / builtin-mv.c
index e47942c13522b2bc8a83203c61071697c9a03307..c0c8764f7fa71ffe459997f03b5158cd7c72209b 100644 (file)
@@ -17,12 +17,19 @@ static const char builtin_mv_usage[] =
 static const char **copy_pathspec(const char *prefix, const char **pathspec,
                                  int count, int base_name)
 {
+       int i;
        const char **result = xmalloc((count + 1) * sizeof(const char *));
        memcpy(result, pathspec, count * sizeof(const char *));
        result[count] = NULL;
-       if (base_name) {
-               int i;
-               for (i = 0; i < count; i++) {
+       for (i = 0; i < count; i++) {
+               int length = strlen(result[i]);
+               if (length > 0 && result[i][length - 1] == '/') {
+                       char *without_slash = xmalloc(length);
+                       memcpy(without_slash, result[i], length - 1);
+                       without_slash[length] = '\0';
+                       result[i] = without_slash;
+               }
+               if (base_name) {
                        const char *last_slash = strrchr(result[i], '/');
                        if (last_slash)
                                result[i] = last_slash + 1;
@@ -48,7 +55,8 @@ static const char *add_slash(const char *path)
        if (path[len - 1] != '/') {
                char *with_slash = xmalloc(len + 2);
                memcpy(with_slash, path, len);
-               strcat(with_slash + len, "/");
+               with_slash[len++] = '/';
+               with_slash[len] = 0;
                return with_slash;
        }
        return path;
@@ -71,10 +79,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
        git_config(git_default_config);
 
-       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
-       if (newfd < 0)
-               die("unable to create new index file");
-
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
        if (read_cache() < 0)
                die("index file corrupt");
 
@@ -121,6 +126,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
        /* Checking */
        for (i = 0; i < count; i++) {
+               int length;
                const char *bad = NULL;
 
                if (show_only)
@@ -130,6 +136,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                if (lstat(source[i], &st) < 0)
                        bad = "bad source";
 
+               if (!bad &&
+                   (length = strlen(source[i])) >= 0 &&
+                   !strncmp(destination[i], source[i], length) &&
+                   (destination[i][length] == 0 || destination[i][length] == '/'))
+                       bad = "can not move directory into itself";
+
                if (S_ISDIR(st.st_mode)) {
                        const char *dir = source[i], *dest_dir = destination[i];
                        int first, last, len = strlen(dir);
@@ -205,10 +217,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                        }
                }
 
-               if (!bad &&
-                   !strncmp(destination[i], source[i], strlen(source[i])))
-                       bad = "can not move directory into itself";
-
                if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
                        bad = "not under version control";