Skip to content

Commit abdd06e

Browse files
committed
Switch to git rev-parse --show-prefix for gitignore pattern prefixes to handle worktrees
For more, see #103
1 parent 19808d6 commit abdd06e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

refresh.template.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ def _ensure_external_workspaces_link_exists():
973973

974974
def _ensure_gitignore_entries_exist():
975975
"""Ensure `//compile_commands.json`, `//external`, and other useful entries are `.gitignore`'d if in a git repo."""
976-
# Silently check if we're (nested) within a git repository. It isn't sufficient to check for the presence of a `.git` directory, in case the bazel workspace is nested inside the git repository.
976+
# Silently check if we're (nested) within a git repository. It isn't sufficient to check for the presence of a `.git` directory, in case, e.g., the bazel workspace is nested inside the git repository or you're off in git worktree.
977977
git_dir_process = subprocess.run('git rev-parse --git-dir',
978978
shell=True, # Ensure this will still fail with a nonzero error code even if `git` isn't installed, unifying error cases.
979979
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
@@ -988,9 +988,14 @@ def _ensure_gitignore_entries_exist():
988988
# Hidden gitignore documented in https://git-scm.com/docs/gitignore
989989
git_dir = pathlib.Path(git_dir_process.stdout.rstrip())
990990
hidden_gitignore_path = git_dir / 'info' / 'exclude'
991-
pattern_prefix = str(pathlib.Path.cwd().relative_to(git_dir.parent.absolute()))
992-
if pattern_prefix == '.': pattern_prefix = ''
993-
elif pattern_prefix: pattern_prefix += '/'
991+
992+
# Get path to the workspace root (current working directory) from the git repository root
993+
git_prefix_process = subprocess.run(['git', 'rev-parse', '--show-prefix'],
994+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
995+
encoding=locale.getpreferredencoding(),
996+
check=True, # Should always succeed if the other did
997+
)
998+
pattern_prefix = git_prefix_process.stdout.rstrip()
994999

9951000
# Each (pattern, explanation) will be added to the `.gitignore` file if the pattern isn't present.
9961001
needed_entries = [

0 commit comments

Comments
 (0)