Skip to content

Commit 4bf812c

Browse files
committed
[githooks] Support git worktrees and relative hooksPath
Currently, setup.py sets core.hooksPath using an absolute filesystem path in the repository configuration. In environments with Git worktrees, repository configuration (.git/config) is shared across all worktrees. Setting core.hooksPath globally in .git/config impacts all worktrees, even those where engine dependencies (such as the Dart SDK fetched via gclient sync) have not been installed. Git operations in unconfigured worktrees (or the root repository) then fail when attempting to run non-existent hook binaries. This change: - Detects if running inside a Git worktree. - Enables extensions.worktreeConfig and applies git config --worktree to scope core.hooksPath strictly to the active worktree. - Uses a relative path from the repository root for core.hooksPath with forward slashes for cross-platform portability.
1 parent 72dc94b commit 4bf812c

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

  • engine/src/flutter/tools/githooks

‎engine/src/flutter/tools/githooks/setup.py‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ def IsWindows():
2323
return os_id.startswith('win32') or os_id.startswith('cygwin')
2424

2525

26+
def is_worktree(git: str, cwd: str) -> bool:
27+
try:
28+
git_dir = subprocess.check_output(
29+
[git, 'rev-parse', '--git-dir'], cwd=cwd, text=True
30+
).strip()
31+
common_dir = subprocess.check_output(
32+
[git, 'rev-parse', '--git-common-dir'], cwd=cwd, text=True
33+
).strip()
34+
return os.path.abspath(os.path.join(cwd, git_dir)) != os.path.abspath(
35+
os.path.join(cwd, common_dir)
36+
)
37+
except (subprocess.CalledProcessError, OSError):
38+
return False
39+
40+
41+
def get_repo_root(git: str, cwd: str) -> str:
42+
try:
43+
return subprocess.check_output(
44+
[git, 'rev-parse', '--show-toplevel'], cwd=cwd, text=True
45+
).strip()
46+
except (subprocess.CalledProcessError, OSError):
47+
return ''
48+
49+
2650
def Main(argv):
2751
parser = argparse.ArgumentParser()
2852

@@ -34,10 +58,31 @@ def Main(argv):
3458
if IsWindows():
3559
git = 'git.bat'
3660

61+
in_worktree = is_worktree(git, FLUTTER_DIR)
62+
if in_worktree:
63+
# In environments with multiple Git worktrees, the repository configuration
64+
# (.git/config) is shared across all worktrees. Setting core.hooksPath
65+
# globally in .git/config would affect all worktrees, even those where
66+
# engine dependencies (such as the Dart SDK fetched via gclient sync) have
67+
# not been installed. Git operations like `git push` or `git rebase` in those
68+
# uninitialized worktrees (or the root repository) would then fail when the
69+
# hooks attempt to run non-existent binaries.
70+
#
71+
# Enabling extensions.worktreeConfig allows scoping core.hooksPath strictly
72+
# to this worktree using `git config --worktree`, keeping other worktrees and
73+
# the main repository unaffected.
74+
subprocess.run(
75+
[git, 'config', 'extensions.worktreeConfig', 'true'],
76+
cwd=FLUTTER_DIR,
77+
check=True,
78+
)
79+
3780
command = [
3881
git,
3982
'config',
4083
]
84+
if in_worktree:
85+
command.append('--worktree')
4186

4287
if args.unset:
4388
command += [
@@ -47,11 +92,14 @@ def Main(argv):
4792
print('Uninstalling Git Hooks')
4893
else:
4994
githooks = os.path.join(FLUTTER_DIR, 'tools', 'githooks')
95+
repo_root = get_repo_root(git, FLUTTER_DIR)
96+
hooks_path = os.path.relpath(githooks, repo_root) if repo_root else githooks
97+
hooks_path = hooks_path.replace(os.sep, '/')
5098
command += [
5199
'core.hooksPath',
52-
githooks,
100+
hooks_path,
53101
]
54-
print('Installing Git Hooks')
102+
print(f'Installing Git Hooks at {hooks_path}')
55103

56104
result = subprocess.run(command, cwd=FLUTTER_DIR)
57105
return result.returncode

0 commit comments

Comments
 (0)