Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dir: fix treat_leading_path() to return false on non-directories #1723

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ivantsepp
Copy link
Contributor

@ivantsepp ivantsepp commented May 20, 2024

Hi there!

I wanted to provide some extra context on the behavior I'm trying to fix. I noticed in one of my git hook scripts that it was showing a warning error message when running git status on a deleted directory. You can reproduce with the following on the git repo:

$ git status --short t/non_existent_directory/
warning: could not open directory 't/non_existent_directory/': No such file or directory

However, this warning doesn't show if you give a non-directory from the root of the repo:

$ git status --short non_existent_directory/

Also doesn't show if you give it a directory in gitignore (/bin is in t/unit-tests/.gitignore)

$ git status --short t/unit-tests/bin/non_existent_directory

I found it strange that sometimes git is able to detect non-directories without warnings. Even stranger, an older version of git didn't show this warning in the first example.

After running git bisect, I was able to track down the commit that introduced this behavior: b9670c1 ("dir: fix checks on common prefix directory", 2019-12-19).

Before, that change, !is_directory(sb.buf) would break out of the loop and return 0 since the branch of code that changes rc to 1 hasn't been reached yet. The change introduces the conditional state == path_recurse as the new return statement. This state variable could be from a previous iteration of the loop which is causing this new behavior. The fix is to ensure the is_directory conditional results in the overall method returning false.

I'm not sure if this is considered a bug or not since it's just a warning message. If this isn't noteworthy, then feel free to ignore this pull request!

@derrickstolee
Copy link
Contributor

Before you submit, you may want to replace your git s ... commands to the full git status ... commands instead of using what I assume is a custom alias.

@ivantsepp
Copy link
Contributor Author

Before you submit, you may want to replace your git s ... commands to the full git status ... commands instead of using what I assume is a custom alias.

Thank you! Great catch! Updated the PR description to use the full command instead of my alias.

If treat_leading_path() encounters a non-directory in its loop over each
leading path component, it should return false. This bug was introduced
in commit b9670c1 ("dir: fix checks on common prefix directory",
2019-12-19) where the check for `is_directory(sb.buf)` still breaks out
of the loop but doesn't return false anymore. Instead, the loop is
broken out and the last state result in the loop is used in the return
conditional: `state == path_recurse`.

This prevents the warning "warning: could not open directory" errors
from occurring in `git status` or `git ls-files -o` calls on
non-directory pathspecs. The warning was introduced in commit b673155
("dir.c: stop ignoring opendir() error in open_cached_dir()",
2018-02-02).

Signed-off-by: Ivan Tse <ivan.tse1@gmail.com>
@ivantsepp
Copy link
Contributor Author

/preview

Copy link

Preview email sent as pull.1723.git.git.1716301467964.gitgitgadget@gmail.com

@ivantsepp
Copy link
Contributor Author

/submit

Copy link

Submitted as pull.1723.git.git.1716306532869.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1723/ivantsepp/itse_untracked_warnings-v1

To fetch this version to local tag pr-git-1723/ivantsepp/itse_untracked_warnings-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1723/ivantsepp/itse_untracked_warnings-v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants