@matheusbsbs / matheus-bagliano / commits / 0bbd0e8b52
Fork 0

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 <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 1, 2020 at 04:17 UTC 0bbd0e8b5233b7cf66b846d4c1825a530d8402bd
1 file changed +17 -18
dir.c
+17 -18
@@ -1660,29 +1660,28 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
1660 const struct pathspec *pathspec)
1661 {
1662 int nested_repo = 0;
1663 -
1663 /* 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);
1665
1669 - case index_gitdir:
1666 + if (status == index_directory)
1667 + return path_recurse;
1668 + if (status == index_gitdir)
1669 return path_none;
1670 + if (status != index_nonexistent)
1671 + BUG("Unhandled value for directory_exists_in_index: %d\n", status);
1672
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));
1683
1684 - if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1685 - break;
1684 + if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) {
1685 if (excluded &&
1686 (dir->flags & DIR_SHOW_IGNORED_TOO) &&
1687 (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {