Skip to content

Commit e2b6cfa

Browse files
pcloudsgitster
authored andcommitted
mv: move index search code out
"Huh?" is removed from die() message. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42de4b1 commit e2b6cfa

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

builtin/mv.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ static void prepare_move_submodule(const char *src, int first,
7878
strbuf_release(&submodule_dotgit);
7979
}
8080

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+
81104
int cmd_mv(int argc, const char **argv, const char *prefix)
82105
{
83106
int i, gitmodules_modified = 0;
@@ -154,25 +177,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
154177
prepare_move_submodule(src, first,
155178
submodule_gitfile + i);
156179
else {
157-
const char *src_w_slash = add_slash(src);
158-
int last, len_w_slash = length + 1;
180+
int last;
159181

160182
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);
176184
if (last - first < 1)
177185
bad = _("source directory is empty");
178186
else {

0 commit comments

Comments
 (0)