mv: move index search code out

"Huh?" is removed from die() message. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Aug 10, 2014 at 09:29 UTC e2b6cfa02e29c4a8f91ede15026b5eaa790de329
1 file changed +25 -17
builtin/mv.c
+25 -17
@@ -78,6 +78,29 @@ static void prepare_move_submodule(const char *src, int first,
78 strbuf_release(&submodule_dotgit);
79 }
80
81 +static int index_range_of_same_dir(const char *src, int length,
82 + int *first_p, int *last_p)
83 +{
84 + const char *src_w_slash = add_slash(src);
85 + int first, last, len_w_slash = length + 1;
86 +
87 + first = cache_name_pos(src_w_slash, len_w_slash);
88 + if (first >= 0)
89 + die(_("%.*s is in index"), len_w_slash, src_w_slash);
90 +
91 + first = -1 - first;
92 + for (last = first; last < active_nr; last++) {
93 + const char *path = active_cache[last]->name;
94 + if (strncmp(path, src_w_slash, len_w_slash))
95 + break;
96 + }
97 + if (src_w_slash != src)
98 + free((char *)src_w_slash);
99 + *first_p = first;
100 + *last_p = last;
101 + return last - first;
102 +}
103 +
104 int cmd_mv(int argc, const char **argv, const char *prefix)
105 {
106 int i, gitmodules_modified = 0;
@@ -154,25 +177,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
177 prepare_move_submodule(src, first,
178 submodule_gitfile + i);
179 else {
157 - const char *src_w_slash = add_slash(src);
158 - int last, len_w_slash = length + 1;
180 + int last;
181
182 modes[i] = WORKING_DIRECTORY;
161 -
162 - first = cache_name_pos(src_w_slash, len_w_slash);
163 - if (first >= 0)
164 - die (_("Huh? %.*s is in index?"),
165 - len_w_slash, src_w_slash);
166 -
167 - first = -1 - first;
168 - for (last = first; last < active_nr; last++) {
169 - const char *path = active_cache[last]->name;
170 - if (strncmp(path, src_w_slash, len_w_slash))
171 - break;
172 - }
173 - if (src_w_slash != src)
174 - free((char *)src_w_slash);
175 -
183 + index_range_of_same_dir(src, length, &first, &last);
184 if (last - first < 1)
185 bad = _("source directory is empty");
186 else {