Skip to content

Commit 0bbd0e8

Browse files
derrickstoleegitster
authored andcommitted
dir: refactor treat_directory to clarify control flow
The logic in treat_directory() is handled by a multi-case switch statement, but this switch is very asymmetrical, as the first two cases are simple but the third is more complicated than the rest of the method. In fact, the third case includes a "break" statement that leads to the block of code outside the switch statement. That is the only way to reach that block, as the switch handles all possible values from directory_exists_in_index(); Extract the switch statement into a series of "if" statements. This simplifies the trivial cases, while clarifying how to reach the "show_other_directories" case. This is particularly important as the "show_other_directories" case will expand in a later change. Helped-by: Elijah Newren <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2df179d commit 0bbd0e8

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

dir.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,29 +1660,28 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
16601660
const struct pathspec *pathspec)
16611661
{
16621662
int nested_repo = 0;
1663-
16641663
/* The "len-1" is to strip the final '/' */
1665-
switch (directory_exists_in_index(istate, dirname, len-1)) {
1666-
case index_directory:
1667-
return path_recurse;
1664+
enum exist_status status = directory_exists_in_index(istate, dirname, len-1);
16681665

1669-
case index_gitdir:
1666+
if (status == index_directory)
1667+
return path_recurse;
1668+
if (status == index_gitdir)
16701669
return path_none;
1670+
if (status != index_nonexistent)
1671+
BUG("Unhandled value for directory_exists_in_index: %d\n", status);
16711672

1672-
case index_nonexistent:
1673-
if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
1674-
!(dir->flags & DIR_NO_GITLINKS)) {
1675-
struct strbuf sb = STRBUF_INIT;
1676-
strbuf_addstr(&sb, dirname);
1677-
nested_repo = is_nonbare_repository_dir(&sb);
1678-
strbuf_release(&sb);
1679-
}
1680-
if (nested_repo)
1681-
return ((dir->flags & DIR_SKIP_NESTED_GIT) ? path_none :
1682-
(excluded ? path_excluded : path_untracked));
1673+
if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
1674+
!(dir->flags & DIR_NO_GITLINKS)) {
1675+
struct strbuf sb = STRBUF_INIT;
1676+
strbuf_addstr(&sb, dirname);
1677+
nested_repo = is_nonbare_repository_dir(&sb);
1678+
strbuf_release(&sb);
1679+
}
1680+
if (nested_repo)
1681+
return ((dir->flags & DIR_SKIP_NESTED_GIT) ? path_none :
1682+
(excluded ? path_excluded : path_untracked));
16831683

1684-
if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1685-
break;
1684+
if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) {
16861685
if (excluded &&
16871686
(dir->flags & DIR_SHOW_IGNORED_TOO) &&
16881687
(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {

0 commit comments

Comments
 (0)