Merge branch 'po/doc-assume-unchanged'
[gitweb.git] / builtin / mv.c
index 3b19ca26b2bb8db5ffb273b7cc40c09ae88e8908..563d05ba1af21c9ae73edceba060161969434c76 100644 (file)
@@ -3,8 +3,8 @@
  *
  * Copyright (C) 2006 Johannes Schindelin
  */
-#include "cache.h"
 #include "builtin.h"
+#include "lockfile.h"
 #include "dir.h"
 #include "cache-tree.h"
 #include "string-list.h"
@@ -78,6 +78,29 @@ static void prepare_move_submodule(const char *src, int first,
        strbuf_release(&submodule_dotgit);
 }
 
+static int index_range_of_same_dir(const char *src, int length,
+                                  int *first_p, int *last_p)
+{
+       const char *src_w_slash = add_slash(src);
+       int first, last, len_w_slash = length + 1;
+
+       first = cache_name_pos(src_w_slash, len_w_slash);
+       if (first >= 0)
+               die(_("%.*s is in index"), len_w_slash, src_w_slash);
+
+       first = -1 - first;
+       for (last = first; last < active_nr; last++) {
+               const char *path = active_cache[last]->name;
+               if (strncmp(path, src_w_slash, len_w_slash))
+                       break;
+       }
+       if (src_w_slash != src)
+               free((char *)src_w_slash);
+       *first_p = first;
+       *last_p = last;
+       return last - first;
+}
+
 int cmd_mv(int argc, const char **argv, const char *prefix)
 {
        int i, gitmodules_modified = 0;
@@ -148,58 +171,36 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                                && lstat(dst, &st) == 0)
                        bad = _("cannot move directory over file");
                else if (src_is_dir) {
-                       int first = cache_name_pos(src, length);
+                       int first = cache_name_pos(src, length), last;
 
                        if (first >= 0)
                                prepare_move_submodule(src, first,
                                                       submodule_gitfile + i);
-                       else {
-                               const char *src_w_slash = add_slash(src);
-                               int last, len_w_slash = length + 1;
+                       else if (index_range_of_same_dir(src, length,
+                                                        &first, &last) < 1)
+                               bad = _("source directory is empty");
+                       else { /* last - first >= 1 */
+                               int j, dst_len, n;
 
                                modes[i] = WORKING_DIRECTORY;
+                               n = argc + last - first;
+                               REALLOC_ARRAY(source, n);
+                               REALLOC_ARRAY(destination, n);
+                               REALLOC_ARRAY(modes, n);
+                               REALLOC_ARRAY(submodule_gitfile, n);
 
-                               first = cache_name_pos(src_w_slash, len_w_slash);
-                               if (first >= 0)
-                                       die (_("Huh? %.*s is in index?"),
-                                                       len_w_slash, src_w_slash);
+                               dst = add_slash(dst);
+                               dst_len = strlen(dst);
 
-                               first = -1 - first;
-                               for (last = first; last < active_nr; last++) {
-                                       const char *path = active_cache[last]->name;
-                                       if (strncmp(path, src_w_slash, len_w_slash))
-                                               break;
-                               }
-                               if (src_w_slash != src)
-                                       free((char *)src_w_slash);
-
-                               if (last - first < 1)
-                                       bad = _("source directory is empty");
-                               else {
-                                       int j, dst_len, n;
-
-                                       n = argc + last - first;
-                                       source = xrealloc(source, n * sizeof(char *));
-                                       destination = xrealloc(destination, n * sizeof(char *));
-                                       modes = xrealloc(modes, n * sizeof(enum update_mode));
-                                       submodule_gitfile =
-                                               xrealloc(submodule_gitfile, n * sizeof(char *));
-
-                                       dst = add_slash(dst);
-                                       dst_len = strlen(dst);
-
-                                       for (j = 0; j < last - first; j++) {
-                                               const char *path =
-                                                       active_cache[first + j]->name;
-                                               source[argc + j] = path;
-                                               destination[argc + j] =
-                                                       prefix_path(dst, dst_len,
-                                                               path + length + 1);
-                                               modes[argc + j] = INDEX;
-                                               submodule_gitfile[argc + j] = NULL;
-                                       }
-                                       argc += last - first;
+                               for (j = 0; j < last - first; j++) {
+                                       const char *path = active_cache[first + j]->name;
+                                       source[argc + j] = path;
+                                       destination[argc + j] =
+                                               prefix_path(dst, dst_len, path + length + 1);
+                                       modes[argc + j] = INDEX;
+                                       submodule_gitfile[argc + j] = NULL;
                                }
+                               argc += last - first;
                        }
                } else if (cache_name_pos(src, length) < 0)
                        bad = _("not under version control");
@@ -228,7 +229,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                if (!bad)
                        continue;
                if (!ignore_errors)
-                       die (_("%s, source=%s, destination=%s"),
+                       die(_("%s, source=%s, destination=%s"),
                             bad, src, dst);
                if (--argc > 0) {
                        int n = argc - i;
@@ -252,7 +253,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                        printf(_("Renaming %s to %s\n"), src, dst);
                if (!show_only && mode != INDEX) {
                        if (rename(src, dst) < 0 && !ignore_errors)
-                               die_errno (_("renaming '%s' failed"), src);
+                               die_errno(_("renaming '%s' failed"), src);
                        if (submodule_gitfile[i]) {
                                if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
                                        connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
@@ -273,10 +274,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
        if (gitmodules_modified)
                stage_updated_gitmodules();
 
-       if (active_cache_changed) {
-               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
-                       die(_("Unable to write new index file"));
-       }
+       if (active_cache_changed &&
+           write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+               die(_("Unable to write new index file"));
 
        return 0;
 }