gitacc-switcher 1.7.0__tar.gz → 1.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gitacc_switcher-1.7.0/gitacc_switcher.egg-info → gitacc_switcher-1.8.0}/PKG-INFO +1 -1
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/__init__.py +1 -1
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/account_manager.py +23 -3
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/hook_manager.py +50 -2
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0/gitacc_switcher.egg-info}/PKG-INFO +1 -1
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/pyproject.toml +1 -1
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/setup.py +1 -1
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_account_manager.py +61 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_hook_manager.py +86 -5
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/LICENSE +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/MANIFEST.in +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/README.md +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/cli.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/completion.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/config_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/rules_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/shell_hook.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/ssh_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher/utils.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/SOURCES.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/dependency_links.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/entry_points.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/requires.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/top_level.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/requirements.txt +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/setup.cfg +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/__init__.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_cli.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_completion.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_config_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_rules_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_shell_hook.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_ssh_manager.py +0 -0
- {gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/tests/test_utils.py +0 -0
|
@@ -611,11 +611,11 @@ class AccountManager:
|
|
|
611
611
|
current_email = current_config.get("email")
|
|
612
612
|
|
|
613
613
|
# Check if they match
|
|
614
|
-
|
|
614
|
+
identity_ok = current_name == expected_name and current_email == expected_email
|
|
615
|
+
if identity_ok:
|
|
615
616
|
echo_color("g", f'✓ Account verified: "{expected_account}"')
|
|
616
617
|
echo_color("g", f" Name: {current_name}")
|
|
617
618
|
echo_color("g", f" Email: {current_email}")
|
|
618
|
-
return True
|
|
619
619
|
else:
|
|
620
620
|
echo_color("r", f"✗ Account mismatch!")
|
|
621
621
|
echo_color("r", "")
|
|
@@ -629,7 +629,27 @@ class AccountManager:
|
|
|
629
629
|
echo_color("r", "")
|
|
630
630
|
echo_color("y", f"Please switch to the correct account:")
|
|
631
631
|
echo_color("y", f" gitacc switch {expected_account}")
|
|
632
|
-
|
|
632
|
+
|
|
633
|
+
self._report_hook_status(repo_path)
|
|
634
|
+
|
|
635
|
+
return identity_ok
|
|
636
|
+
|
|
637
|
+
def _report_hook_status(self, repo_path: Optional[Path] = None) -> None:
|
|
638
|
+
"""Print the gitacc pre-commit hook's install status as part of 'verify'."""
|
|
639
|
+
hook_status = self.hook_manager.get_hook_status(repo_path)
|
|
640
|
+
if hook_status == "present":
|
|
641
|
+
echo_color("g", "✓ Pre-commit hook: installed and unmodified")
|
|
642
|
+
elif hook_status == "modified":
|
|
643
|
+
echo_color(
|
|
644
|
+
"y",
|
|
645
|
+
"⚠ Pre-commit hook: present but modified "
|
|
646
|
+
"(edited manually, or replaced by another tool)",
|
|
647
|
+
)
|
|
648
|
+
else:
|
|
649
|
+
echo_color(
|
|
650
|
+
"y",
|
|
651
|
+
"⚠ Pre-commit hook: missing " '(run "gitacc init" to reinstall it)',
|
|
652
|
+
)
|
|
633
653
|
|
|
634
654
|
def export_accounts(self, output_path: Path, include_keys: bool = False) -> bool:
|
|
635
655
|
"""Export registered accounts' metadata to a portable JSON file.
|
|
@@ -104,6 +104,12 @@ exit 0
|
|
|
104
104
|
def get_hooks_dir(self, repo_path: Optional[Path] = None) -> Optional[Path]:
|
|
105
105
|
"""Get the Git hooks directory for a repository.
|
|
106
106
|
|
|
107
|
+
Honors `core.hooksPath` when the repository already has one configured
|
|
108
|
+
(as set by tools like husky or lefthook), so gitacc installs its hook
|
|
109
|
+
where Git will actually run it instead of silently writing into
|
|
110
|
+
`.git/hooks` and never being invoked. Falls back to `.git/hooks` when
|
|
111
|
+
`core.hooksPath` isn't set.
|
|
112
|
+
|
|
107
113
|
Args:
|
|
108
114
|
repo_path: Path to repository (default: current directory)
|
|
109
115
|
|
|
@@ -125,8 +131,20 @@ exit 0
|
|
|
125
131
|
if not git_dir.is_absolute():
|
|
126
132
|
git_dir = repo_path / git_dir
|
|
127
133
|
|
|
128
|
-
|
|
129
|
-
|
|
134
|
+
hooks_path_result = subprocess.run(
|
|
135
|
+
["git", "config", "--get", "core.hooksPath"],
|
|
136
|
+
cwd=repo_path,
|
|
137
|
+
capture_output=True,
|
|
138
|
+
text=True,
|
|
139
|
+
)
|
|
140
|
+
configured_hooks_path = hooks_path_result.stdout.strip()
|
|
141
|
+
if hooks_path_result.returncode == 0 and configured_hooks_path:
|
|
142
|
+
hooks_dir = Path(configured_hooks_path)
|
|
143
|
+
if not hooks_dir.is_absolute():
|
|
144
|
+
hooks_dir = repo_path / hooks_dir
|
|
145
|
+
return hooks_dir
|
|
146
|
+
|
|
147
|
+
return git_dir / "hooks"
|
|
130
148
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
131
149
|
return None
|
|
132
150
|
|
|
@@ -228,3 +246,33 @@ exit 0
|
|
|
228
246
|
return "Git Account Switcher" in content
|
|
229
247
|
except Exception:
|
|
230
248
|
return False
|
|
249
|
+
|
|
250
|
+
def get_hook_status(self, repo_path: Optional[Path] = None) -> str:
|
|
251
|
+
"""Check whether the gitacc pre-commit hook is present and unmodified.
|
|
252
|
+
|
|
253
|
+
Args:
|
|
254
|
+
repo_path: Path to repository (default: current directory)
|
|
255
|
+
|
|
256
|
+
Returns:
|
|
257
|
+
"present" if the hook file matches what gitacc installs,
|
|
258
|
+
"modified" if a pre-commit hook file exists but its content has
|
|
259
|
+
changed (edited manually, or replaced by another tool),
|
|
260
|
+
"missing" if no pre-commit hook file exists at all.
|
|
261
|
+
"""
|
|
262
|
+
hooks_dir = self.get_hooks_dir(repo_path)
|
|
263
|
+
if not hooks_dir:
|
|
264
|
+
return "missing"
|
|
265
|
+
|
|
266
|
+
hook_file = hooks_dir / "pre-commit"
|
|
267
|
+
if not hook_file.exists():
|
|
268
|
+
return "missing"
|
|
269
|
+
|
|
270
|
+
try:
|
|
271
|
+
with open(hook_file, "r") as f:
|
|
272
|
+
content = f.read()
|
|
273
|
+
except Exception:
|
|
274
|
+
return "modified"
|
|
275
|
+
|
|
276
|
+
if content == self.PRE_COMMIT_HOOK_TEMPLATE:
|
|
277
|
+
return "present"
|
|
278
|
+
return "modified"
|
|
@@ -9,7 +9,7 @@ long_description = readme_file.read_text() if readme_file.exists() else ""
|
|
|
9
9
|
|
|
10
10
|
setup(
|
|
11
11
|
name="gitacc-switcher",
|
|
12
|
-
version="1.
|
|
12
|
+
version="1.8.0",
|
|
13
13
|
description="Git Account Switcher - Manage multiple Git SSH accounts easily",
|
|
14
14
|
long_description=long_description,
|
|
15
15
|
long_description_content_type="text/markdown",
|
|
@@ -536,6 +536,67 @@ class TestGetStatus:
|
|
|
536
536
|
assert status["matched_key_account"] is None
|
|
537
537
|
|
|
538
538
|
|
|
539
|
+
class TestVerifyAccount:
|
|
540
|
+
def test_fails_when_not_a_git_repo(self, manager):
|
|
541
|
+
manager.config_manager.is_git_repo.return_value = False
|
|
542
|
+
|
|
543
|
+
assert manager.verify_account() is False
|
|
544
|
+
manager.hook_manager.get_hook_status.assert_not_called()
|
|
545
|
+
|
|
546
|
+
def test_true_when_no_expected_account_configured(self, manager):
|
|
547
|
+
manager.config_manager.is_git_repo.return_value = True
|
|
548
|
+
manager.config_manager.get_repo_expected_account.return_value = None
|
|
549
|
+
|
|
550
|
+
assert manager.verify_account() is True
|
|
551
|
+
manager.hook_manager.get_hook_status.assert_not_called()
|
|
552
|
+
|
|
553
|
+
def test_fails_when_expected_account_unregistered(self, manager):
|
|
554
|
+
manager.config_manager.is_git_repo.return_value = True
|
|
555
|
+
manager.config_manager.get_repo_expected_account.return_value = "work"
|
|
556
|
+
manager.config_manager.get_account.return_value = None
|
|
557
|
+
|
|
558
|
+
assert manager.verify_account() is False
|
|
559
|
+
manager.hook_manager.get_hook_status.assert_not_called()
|
|
560
|
+
|
|
561
|
+
def test_true_on_identity_match_and_reports_hook_present(self, manager):
|
|
562
|
+
manager.config_manager.is_git_repo.return_value = True
|
|
563
|
+
manager.config_manager.get_repo_expected_account.return_value = "work"
|
|
564
|
+
manager.config_manager.get_account.return_value = WORK_ACCOUNT
|
|
565
|
+
manager.config_manager.get_current_git_config_local.return_value = {
|
|
566
|
+
"name": "Work User",
|
|
567
|
+
"email": "work@company.com",
|
|
568
|
+
}
|
|
569
|
+
manager.hook_manager.get_hook_status.return_value = "present"
|
|
570
|
+
|
|
571
|
+
assert manager.verify_account() is True
|
|
572
|
+
manager.hook_manager.get_hook_status.assert_called_once_with(None)
|
|
573
|
+
|
|
574
|
+
def test_true_on_identity_match_even_when_hook_missing(self, manager):
|
|
575
|
+
manager.config_manager.is_git_repo.return_value = True
|
|
576
|
+
manager.config_manager.get_repo_expected_account.return_value = "work"
|
|
577
|
+
manager.config_manager.get_account.return_value = WORK_ACCOUNT
|
|
578
|
+
manager.config_manager.get_current_git_config_local.return_value = {
|
|
579
|
+
"name": "Work User",
|
|
580
|
+
"email": "work@company.com",
|
|
581
|
+
}
|
|
582
|
+
manager.hook_manager.get_hook_status.return_value = "missing"
|
|
583
|
+
|
|
584
|
+
assert manager.verify_account() is True
|
|
585
|
+
|
|
586
|
+
def test_false_on_identity_mismatch_still_reports_hook_status(self, manager):
|
|
587
|
+
manager.config_manager.is_git_repo.return_value = True
|
|
588
|
+
manager.config_manager.get_repo_expected_account.return_value = "work"
|
|
589
|
+
manager.config_manager.get_account.return_value = WORK_ACCOUNT
|
|
590
|
+
manager.config_manager.get_current_git_config_local.return_value = {
|
|
591
|
+
"name": "Someone Else",
|
|
592
|
+
"email": "someone@else.com",
|
|
593
|
+
}
|
|
594
|
+
manager.hook_manager.get_hook_status.return_value = "modified"
|
|
595
|
+
|
|
596
|
+
assert manager.verify_account() is False
|
|
597
|
+
manager.hook_manager.get_hook_status.assert_called_once_with(None)
|
|
598
|
+
|
|
599
|
+
|
|
539
600
|
class TestRunDoctor:
|
|
540
601
|
def test_agent_running_and_all_keys_ok(self, manager, tmp_path):
|
|
541
602
|
key = tmp_path / "id_rsa_work"
|
|
@@ -19,12 +19,26 @@ def hook_manager():
|
|
|
19
19
|
return HookManager()
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
def
|
|
22
|
+
def _git_subprocess(configured_hooks_path=None):
|
|
23
|
+
"""Fake `subprocess.run` that answers `git rev-parse --git-dir` and
|
|
24
|
+
`git config --get core.hooksPath`, mirroring real git's behavior of a
|
|
25
|
+
non-zero exit with empty output when core.hooksPath isn't set."""
|
|
26
|
+
|
|
27
|
+
def _run(cmd, **kwargs):
|
|
28
|
+
if cmd[:2] == ["git", "rev-parse"]:
|
|
29
|
+
return MagicMock(returncode=0, stdout=".git\n")
|
|
30
|
+
if cmd[:2] == ["git", "config"]:
|
|
31
|
+
if configured_hooks_path is not None:
|
|
32
|
+
return MagicMock(returncode=0, stdout=f"{configured_hooks_path}\n")
|
|
33
|
+
return MagicMock(returncode=1, stdout="")
|
|
34
|
+
raise AssertionError(f"unexpected subprocess.run call: {cmd}")
|
|
35
|
+
|
|
36
|
+
return _run
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _mock_git_dir(repo_path, configured_hooks_path=None):
|
|
23
40
|
"""Return a patch context that makes get_hooks_dir resolve correctly."""
|
|
24
|
-
return patch(
|
|
25
|
-
"subprocess.run",
|
|
26
|
-
return_value=MagicMock(returncode=0, stdout=".git\n"),
|
|
27
|
-
)
|
|
41
|
+
return patch("subprocess.run", side_effect=_git_subprocess(configured_hooks_path))
|
|
28
42
|
|
|
29
43
|
|
|
30
44
|
class TestGetHooksDir:
|
|
@@ -42,6 +56,19 @@ class TestGetHooksDir:
|
|
|
42
56
|
):
|
|
43
57
|
assert hook_manager.get_hooks_dir(tmp_path) is None
|
|
44
58
|
|
|
59
|
+
def test_honors_configured_core_hooks_path(self, hook_manager, git_repo):
|
|
60
|
+
(git_repo / "custom-hooks").mkdir()
|
|
61
|
+
with _mock_git_dir(git_repo, configured_hooks_path="custom-hooks"):
|
|
62
|
+
result = hook_manager.get_hooks_dir(git_repo)
|
|
63
|
+
assert result == git_repo / "custom-hooks"
|
|
64
|
+
|
|
65
|
+
def test_falls_back_to_git_hooks_when_hooks_path_unset(
|
|
66
|
+
self, hook_manager, git_repo
|
|
67
|
+
):
|
|
68
|
+
with _mock_git_dir(git_repo, configured_hooks_path=None):
|
|
69
|
+
result = hook_manager.get_hooks_dir(git_repo)
|
|
70
|
+
assert result == git_repo / ".git" / "hooks"
|
|
71
|
+
|
|
45
72
|
|
|
46
73
|
class TestInstallPreCommitHook:
|
|
47
74
|
def test_creates_hook_file(self, hook_manager, git_repo):
|
|
@@ -82,6 +109,28 @@ class TestInstallPreCommitHook:
|
|
|
82
109
|
result = hook_manager.install_pre_commit_hook(git_repo)
|
|
83
110
|
assert result is True
|
|
84
111
|
|
|
112
|
+
def test_installs_into_configured_hooks_path(self, hook_manager, git_repo):
|
|
113
|
+
"""When core.hooksPath already points elsewhere (e.g. husky, lefthook),
|
|
114
|
+
gitacc must install there instead of .git/hooks, where git would
|
|
115
|
+
otherwise silently never run it."""
|
|
116
|
+
with _mock_git_dir(git_repo, configured_hooks_path="custom-hooks"):
|
|
117
|
+
assert hook_manager.install_pre_commit_hook(git_repo) is True
|
|
118
|
+
assert (git_repo / "custom-hooks" / "pre-commit").exists()
|
|
119
|
+
assert not (git_repo / ".git" / "hooks" / "pre-commit").exists()
|
|
120
|
+
|
|
121
|
+
def test_refuses_to_overwrite_foreign_hook_in_configured_hooks_path(
|
|
122
|
+
self, hook_manager, git_repo
|
|
123
|
+
):
|
|
124
|
+
custom_hooks = git_repo / "custom-hooks"
|
|
125
|
+
custom_hooks.mkdir()
|
|
126
|
+
hook = custom_hooks / "pre-commit"
|
|
127
|
+
hook.write_text("#!/bin/sh\necho 'husky'\n")
|
|
128
|
+
hook.chmod(0o755)
|
|
129
|
+
with _mock_git_dir(git_repo, configured_hooks_path="custom-hooks"):
|
|
130
|
+
result = hook_manager.install_pre_commit_hook(git_repo)
|
|
131
|
+
assert result is False
|
|
132
|
+
assert "husky" in hook.read_text() # original preserved
|
|
133
|
+
|
|
85
134
|
|
|
86
135
|
class TestUninstallPreCommitHook:
|
|
87
136
|
def test_removes_own_hook(self, hook_manager, git_repo):
|
|
@@ -120,3 +169,35 @@ class TestIsHookInstalled:
|
|
|
120
169
|
hook.write_text("#!/bin/sh\necho foreign\n")
|
|
121
170
|
with _mock_git_dir(git_repo):
|
|
122
171
|
assert hook_manager.is_hook_installed(git_repo) is False
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class TestGetHookStatus:
|
|
175
|
+
def test_present_when_freshly_installed(self, hook_manager, git_repo):
|
|
176
|
+
with _mock_git_dir(git_repo):
|
|
177
|
+
hook_manager.install_pre_commit_hook(git_repo)
|
|
178
|
+
assert hook_manager.get_hook_status(git_repo) == "present"
|
|
179
|
+
|
|
180
|
+
def test_missing_when_no_hook_file(self, hook_manager, git_repo):
|
|
181
|
+
with _mock_git_dir(git_repo):
|
|
182
|
+
assert hook_manager.get_hook_status(git_repo) == "missing"
|
|
183
|
+
|
|
184
|
+
def test_missing_when_not_a_git_repo(self, hook_manager, tmp_path):
|
|
185
|
+
import subprocess
|
|
186
|
+
|
|
187
|
+
with patch(
|
|
188
|
+
"subprocess.run", side_effect=subprocess.CalledProcessError(128, "git")
|
|
189
|
+
):
|
|
190
|
+
assert hook_manager.get_hook_status(tmp_path) == "missing"
|
|
191
|
+
|
|
192
|
+
def test_modified_when_edited_after_install(self, hook_manager, git_repo):
|
|
193
|
+
with _mock_git_dir(git_repo):
|
|
194
|
+
hook_manager.install_pre_commit_hook(git_repo)
|
|
195
|
+
hook = git_repo / ".git" / "hooks" / "pre-commit"
|
|
196
|
+
hook.write_text(hook.read_text() + "\n# manual edit\n")
|
|
197
|
+
assert hook_manager.get_hook_status(git_repo) == "modified"
|
|
198
|
+
|
|
199
|
+
def test_modified_when_replaced_by_foreign_hook(self, hook_manager, git_repo):
|
|
200
|
+
hook = git_repo / ".git" / "hooks" / "pre-commit"
|
|
201
|
+
hook.write_text("#!/bin/sh\necho 'other tool'\n")
|
|
202
|
+
with _mock_git_dir(git_repo):
|
|
203
|
+
assert hook_manager.get_hook_status(git_repo) == "modified"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{gitacc_switcher-1.7.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|