dir: refactor treat_directory to clarify control flow
authorDerrick Stolee <redacted>
Wed, 1 Apr 2020 04:17:41 +0000 (04:17 +0000)
committerJunio C Hamano <redacted>
Wed, 1 Apr 2020 18:10:38 +0000 (11:10 -0700)
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 <redacted>
Signed-off-by: Derrick Stolee <redacted>
Signed-off-by: Elijah Newren <redacted>
Signed-off-by: Junio C Hamano <redacted>
dir.c

diff --git a/dir.c b/dir.c
index 8074e651e6feb67ec9fc81c3de2042239fb809ca..d9bcb7e19b610a085e2b38bba1366dce28e60d47 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1660,29 +1660,28 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
        const struct pathspec *pathspec)
 {
        int nested_repo = 0;
-
        /* The "len-1" is to strip the final '/' */
-       switch (directory_exists_in_index(istate, dirname, len-1)) {
-       case index_directory:
-               return path_recurse;
+       enum exist_status status = directory_exists_in_index(istate, dirname, len-1);
 
-       case index_gitdir:
+       if (status == index_directory)
+               return path_recurse;
+       if (status == index_gitdir)
                return path_none;
+       if (status != index_nonexistent)
+               BUG("Unhandled value for directory_exists_in_index: %d\n", status);
 
-       case index_nonexistent:
-               if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
-                   !(dir->flags & DIR_NO_GITLINKS)) {
-                       struct strbuf sb = STRBUF_INIT;
-                       strbuf_addstr(&sb, dirname);
-                       nested_repo = is_nonbare_repository_dir(&sb);
-                       strbuf_release(&sb);
-               }
-               if (nested_repo)
-                       return ((dir->flags & DIR_SKIP_NESTED_GIT) ? path_none :
-                               (excluded ? path_excluded : path_untracked));
+       if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
+               !(dir->flags & DIR_NO_GITLINKS)) {
+               struct strbuf sb = STRBUF_INIT;
+               strbuf_addstr(&sb, dirname);
+               nested_repo = is_nonbare_repository_dir(&sb);
+               strbuf_release(&sb);
+       }
+       if (nested_repo)
+               return ((dir->flags & DIR_SKIP_NESTED_GIT) ? path_none :
+                       (excluded ? path_excluded : path_untracked));
 
-               if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
-                       break;
+       if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) {
                if (excluded &&
                    (dir->flags & DIR_SHOW_IGNORED_TOO) &&
                    (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) {
git clone https://git.99rst.org/PROJECT