Skip to content

Commit d58a687

Browse files
peffgitster
authored andcommitted
mv: factor out empty src_dir removal
This pulls the loop added by b6f51e3 (mv: cleanup empty WORKING_DIRECTORY, 2022-08-09) into a sub-function. That reduces clutter in cmd_mv() and makes it easier to see that the lifetime of the a_src_dir strbuf is limited to this code (and thus its cleanup doesn't need to go after the "out" label). Another option would be to just declare the strbuf inside the loop, since it is only used there. But this refactor retains the existing property that we can reuse the allocated buffer for each iteration of the loop. That optimization is probably overkill, but I think the sub-function is more readable anyway, and then keeping the optimization is basically free. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc65e08 commit d58a687

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

builtin/mv.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ static int empty_dir_has_sparse_contents(const char *name)
156156
return ret;
157157
}
158158

159+
static void remove_empty_src_dirs(const char **src_dir, size_t src_dir_nr)
160+
{
161+
size_t i;
162+
struct strbuf a_src_dir = STRBUF_INIT;
163+
164+
for (i = 0; i < src_dir_nr; i++) {
165+
int dummy;
166+
strbuf_addstr(&a_src_dir, src_dir[i]);
167+
/*
168+
* if entries under a_src_dir are all moved away,
169+
* recursively remove a_src_dir to cleanup
170+
*/
171+
if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
172+
&dummy, &dummy) < 1) {
173+
remove_dir_recursively(&a_src_dir, 0);
174+
}
175+
strbuf_reset(&a_src_dir);
176+
}
177+
178+
strbuf_release(&a_src_dir);
179+
}
180+
159181
int cmd_mv(int argc, const char **argv, const char *prefix)
160182
{
161183
int i, flags, gitmodules_modified = 0;
@@ -177,7 +199,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
177199
char *dst_w_slash = NULL;
178200
const char **src_dir = NULL;
179201
int src_dir_nr = 0, src_dir_alloc = 0;
180-
struct strbuf a_src_dir = STRBUF_INIT;
181202
enum update_mode *modes, dst_mode = 0;
182203
struct stat st, dest_st;
183204
struct string_list src_for_dst = STRING_LIST_INIT_DUP;
@@ -538,24 +559,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
538559
}
539560
}
540561

541-
/*
542-
* cleanup the empty src_dirs
543-
*/
544-
for (i = 0; i < src_dir_nr; i++) {
545-
int dummy;
546-
strbuf_addstr(&a_src_dir, src_dir[i]);
547-
/*
548-
* if entries under a_src_dir are all moved away,
549-
* recursively remove a_src_dir to cleanup
550-
*/
551-
if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
552-
&dummy, &dummy) < 1) {
553-
remove_dir_recursively(&a_src_dir, 0);
554-
}
555-
strbuf_reset(&a_src_dir);
556-
}
557-
558-
strbuf_release(&a_src_dir);
562+
remove_empty_src_dirs(src_dir, src_dir_nr);
559563

560564
if (dirty_paths.nr)
561565
advise_on_moving_dirty_path(&dirty_paths);

0 commit comments

Comments
 (0)