pw_watch: Properly handle deleted directories
Have git_ignored find the Git repo a deleted directory would have been
in, and ask that repo if the path was ignored.
Change-Id: If1db0ab6900a334336705dbcbd437335e3387b31
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/42262
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_watch/py/pw_watch/watch.py b/pw_watch/py/pw_watch/watch.py
index c7269e7..87acd7f 100755
--- a/pw_watch/py/pw_watch/watch.py
+++ b/pw_watch/py/pw_watch/watch.py
@@ -125,12 +125,27 @@
Returns true for ignored files that were manually added to a repo.
"""
- returncode = subprocess.run(
- ['git', 'check-ignore', '--quiet', '--no-index', file],
- stdout=subprocess.DEVNULL,
- stderr=subprocess.DEVNULL,
- cwd=file.parent).returncode
- return returncode in (0, 128)
+ file = file.resolve()
+ directory = file.parent
+
+ # Run the Git command from file's parent so that the correct repo is used.
+ while True:
+ try:
+ returncode = subprocess.run(
+ ['git', 'check-ignore', '--quiet', '--no-index', file],
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ cwd=directory).returncode
+ return returncode in (0, 128)
+ except FileNotFoundError:
+ # If the directory no longer exists, try parent directories until
+ # an existing directory is found or all directories have been
+ # checked. This approach makes it possible to check if a deleted
+ # path is ignored in the repo it was originally created in.
+ if directory == directory.parent:
+ return False
+
+ directory = directory.parent
class PigweedBuildWatcher(FileSystemEventHandler, DebouncedFunction):