gitacc-switcher 1.6.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.6.0/gitacc_switcher.egg-info → gitacc_switcher-1.8.0}/PKG-INFO +1 -1
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/__init__.py +1 -1
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/account_manager.py +235 -2
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/cli.py +62 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/hook_manager.py +50 -2
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/ssh_manager.py +20 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0/gitacc_switcher.egg-info}/PKG-INFO +1 -1
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/pyproject.toml +1 -1
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/setup.py +1 -1
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_account_manager.py +362 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_cli.py +61 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_hook_manager.py +86 -5
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_ssh_manager.py +14 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/LICENSE +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/MANIFEST.in +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/README.md +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/completion.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/config_manager.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/rules_manager.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/shell_hook.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher/utils.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/SOURCES.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/dependency_links.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/entry_points.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/requires.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/gitacc_switcher.egg-info/top_level.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/requirements.txt +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/setup.cfg +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/__init__.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_completion.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_config_manager.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_rules_manager.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_shell_hook.py +0 -0
- {gitacc_switcher-1.6.0 → gitacc_switcher-1.8.0}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Account management operations."""
|
|
2
2
|
|
|
3
3
|
import getpass
|
|
4
|
+
import json
|
|
4
5
|
import subprocess
|
|
5
6
|
from pathlib import Path
|
|
6
7
|
from typing import Optional, Dict, List
|
|
@@ -610,11 +611,11 @@ class AccountManager:
|
|
|
610
611
|
current_email = current_config.get("email")
|
|
611
612
|
|
|
612
613
|
# Check if they match
|
|
613
|
-
|
|
614
|
+
identity_ok = current_name == expected_name and current_email == expected_email
|
|
615
|
+
if identity_ok:
|
|
614
616
|
echo_color("g", f'✓ Account verified: "{expected_account}"')
|
|
615
617
|
echo_color("g", f" Name: {current_name}")
|
|
616
618
|
echo_color("g", f" Email: {current_email}")
|
|
617
|
-
return True
|
|
618
619
|
else:
|
|
619
620
|
echo_color("r", f"✗ Account mismatch!")
|
|
620
621
|
echo_color("r", "")
|
|
@@ -628,4 +629,236 @@ class AccountManager:
|
|
|
628
629
|
echo_color("r", "")
|
|
629
630
|
echo_color("y", f"Please switch to the correct account:")
|
|
630
631
|
echo_color("y", f" gitacc switch {expected_account}")
|
|
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
|
+
)
|
|
653
|
+
|
|
654
|
+
def export_accounts(self, output_path: Path, include_keys: bool = False) -> bool:
|
|
655
|
+
"""Export registered accounts' metadata to a portable JSON file.
|
|
656
|
+
|
|
657
|
+
Metadata-only by default (name, email, sign preference, key *paths*,
|
|
658
|
+
and any host aliases bound to each account's key) — no key material
|
|
659
|
+
is read or written unless `include_keys` is explicitly set.
|
|
660
|
+
|
|
661
|
+
Args:
|
|
662
|
+
output_path: File to write the export to
|
|
663
|
+
include_keys: If True, also embed each account's private/public
|
|
664
|
+
SSH key file contents in the export. The resulting file will
|
|
665
|
+
contain private key material in plain text; it is written
|
|
666
|
+
with 0600 permissions, but the caller is responsible for
|
|
667
|
+
keeping it secure afterwards.
|
|
668
|
+
|
|
669
|
+
Returns:
|
|
670
|
+
True if successful, False otherwise
|
|
671
|
+
"""
|
|
672
|
+
accounts = self.config_manager.read_accounts()
|
|
673
|
+
if not accounts:
|
|
674
|
+
echo_color("y", "No accounts registered yet. Nothing to export.")
|
|
675
|
+
return False
|
|
676
|
+
|
|
677
|
+
host_aliases = self.ssh_manager.list_host_aliases()
|
|
678
|
+
|
|
679
|
+
exported_accounts = {}
|
|
680
|
+
for account_name, info in accounts.items():
|
|
681
|
+
private_key = info.get("private_key", "")
|
|
682
|
+
public_key = info.get("public_key", "")
|
|
683
|
+
|
|
684
|
+
entry = {
|
|
685
|
+
"name": info.get("name", account_name),
|
|
686
|
+
"email": info.get("email", ""),
|
|
687
|
+
"sign": info.get("sign", "false"),
|
|
688
|
+
"private_key": private_key,
|
|
689
|
+
"public_key": public_key,
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
matching_aliases = {
|
|
693
|
+
alias: {"hostname": alias_info["hostname"], "user": alias_info["user"]}
|
|
694
|
+
for alias, alias_info in host_aliases.items()
|
|
695
|
+
if private_key and alias_info.get("identity_file") == private_key
|
|
696
|
+
}
|
|
697
|
+
if matching_aliases:
|
|
698
|
+
entry["host_aliases"] = matching_aliases
|
|
699
|
+
|
|
700
|
+
if include_keys:
|
|
701
|
+
if public_key:
|
|
702
|
+
public_key_content = self.ssh_manager.get_public_key_content(
|
|
703
|
+
public_key
|
|
704
|
+
)
|
|
705
|
+
if public_key_content:
|
|
706
|
+
entry["public_key_content"] = public_key_content
|
|
707
|
+
if private_key:
|
|
708
|
+
private_key_content = self.ssh_manager.get_private_key_content(
|
|
709
|
+
private_key
|
|
710
|
+
)
|
|
711
|
+
if private_key_content:
|
|
712
|
+
entry["private_key_content"] = private_key_content
|
|
713
|
+
|
|
714
|
+
exported_accounts[account_name] = entry
|
|
715
|
+
|
|
716
|
+
export_data = {
|
|
717
|
+
"version": 1,
|
|
718
|
+
"include_keys": include_keys,
|
|
719
|
+
"accounts": exported_accounts,
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
output_path = Path(output_path)
|
|
723
|
+
try:
|
|
724
|
+
with open(output_path, "w") as f:
|
|
725
|
+
json.dump(export_data, f, indent=2)
|
|
726
|
+
f.write("\n")
|
|
727
|
+
if include_keys:
|
|
728
|
+
output_path.chmod(0o600)
|
|
729
|
+
except OSError as e:
|
|
730
|
+
echo_color("r", f"Failed to write export file: {e}")
|
|
731
|
+
return False
|
|
732
|
+
|
|
733
|
+
echo_color(
|
|
734
|
+
"g",
|
|
735
|
+
f'Exported {len(exported_accounts)} account(s) to "{output_path}"',
|
|
736
|
+
)
|
|
737
|
+
if include_keys:
|
|
738
|
+
echo_color(
|
|
739
|
+
"y",
|
|
740
|
+
"This file contains private key material in plain text — "
|
|
741
|
+
"keep it secure (e.g. encrypt it or delete it once transferred).",
|
|
742
|
+
)
|
|
743
|
+
else:
|
|
744
|
+
echo_color(
|
|
745
|
+
"y",
|
|
746
|
+
"Metadata only: key files themselves were not copied. Pass "
|
|
747
|
+
"--include-keys to also embed key material.",
|
|
748
|
+
)
|
|
749
|
+
return True
|
|
750
|
+
|
|
751
|
+
def _write_imported_key_file(
|
|
752
|
+
self, path_str: str, content: str, mode: int, force: bool
|
|
753
|
+
) -> bool:
|
|
754
|
+
"""Write key material from an import file to disk, if safe to do so.
|
|
755
|
+
|
|
756
|
+
Args:
|
|
757
|
+
path_str: Destination path for the key file
|
|
758
|
+
content: Key file content to write
|
|
759
|
+
mode: File permission mode to set (e.g. 0o600 for a private key)
|
|
760
|
+
force: If True, overwrite an existing file at that path
|
|
761
|
+
|
|
762
|
+
Returns:
|
|
763
|
+
True if the file was written, False if skipped or on error
|
|
764
|
+
"""
|
|
765
|
+
if not path_str:
|
|
766
|
+
return False
|
|
767
|
+
path = Path(path_str)
|
|
768
|
+
if path.exists() and not force:
|
|
769
|
+
return False
|
|
770
|
+
try:
|
|
771
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
772
|
+
path.write_text(content)
|
|
773
|
+
path.chmod(mode)
|
|
774
|
+
return True
|
|
775
|
+
except OSError:
|
|
776
|
+
return False
|
|
777
|
+
|
|
778
|
+
def import_accounts(self, input_path: Path, force: bool = False) -> bool:
|
|
779
|
+
"""Import accounts previously written by `export_accounts`.
|
|
780
|
+
|
|
781
|
+
Args:
|
|
782
|
+
input_path: File previously created with `gitacc export`
|
|
783
|
+
force: If True, overwrite existing accounts, host aliases, and
|
|
784
|
+
key files with the same name/path. Without it, anything
|
|
785
|
+
that already exists is left untouched and skipped.
|
|
786
|
+
|
|
787
|
+
Returns:
|
|
788
|
+
True if the file was read and processed, False if the file
|
|
789
|
+
could not be read or contained no accounts
|
|
790
|
+
"""
|
|
791
|
+
input_path = Path(input_path)
|
|
792
|
+
if not input_path.exists():
|
|
793
|
+
echo_color("r", f'Import file not found: "{input_path}"')
|
|
794
|
+
return False
|
|
795
|
+
|
|
796
|
+
try:
|
|
797
|
+
with open(input_path, "r") as f:
|
|
798
|
+
data = json.load(f)
|
|
799
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
800
|
+
echo_color("r", f"Failed to read import file: {e}")
|
|
801
|
+
return False
|
|
802
|
+
|
|
803
|
+
accounts = data.get("accounts", {})
|
|
804
|
+
if not accounts:
|
|
805
|
+
echo_color("y", "No accounts found in import file.")
|
|
631
806
|
return False
|
|
807
|
+
|
|
808
|
+
imported = 0
|
|
809
|
+
skipped = 0
|
|
810
|
+
|
|
811
|
+
for account_name, entry in accounts.items():
|
|
812
|
+
if self.account_exists(account_name) and not force:
|
|
813
|
+
echo_color(
|
|
814
|
+
"y",
|
|
815
|
+
f'Skipping "{account_name}": account already exists '
|
|
816
|
+
"(use --force to overwrite).",
|
|
817
|
+
)
|
|
818
|
+
skipped += 1
|
|
819
|
+
continue
|
|
820
|
+
|
|
821
|
+
private_key = entry.get("private_key", "")
|
|
822
|
+
public_key = entry.get("public_key", "")
|
|
823
|
+
|
|
824
|
+
private_key_content = entry.get("private_key_content")
|
|
825
|
+
if private_key_content:
|
|
826
|
+
self._write_imported_key_file(
|
|
827
|
+
private_key, private_key_content, 0o600, force
|
|
828
|
+
)
|
|
829
|
+
public_key_content = entry.get("public_key_content")
|
|
830
|
+
if public_key_content:
|
|
831
|
+
self._write_imported_key_file(
|
|
832
|
+
public_key, public_key_content, 0o644, force
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
if self.account_exists(account_name):
|
|
836
|
+
self.config_manager.remove_account(account_name)
|
|
837
|
+
|
|
838
|
+
success = self.config_manager.add_account(
|
|
839
|
+
account_name,
|
|
840
|
+
entry.get("name", account_name),
|
|
841
|
+
entry.get("email", ""),
|
|
842
|
+
private_key,
|
|
843
|
+
public_key,
|
|
844
|
+
sign=str(entry.get("sign", "false")).lower() == "true",
|
|
845
|
+
)
|
|
846
|
+
if not success:
|
|
847
|
+
echo_color("r", f'Failed to import account "{account_name}".')
|
|
848
|
+
continue
|
|
849
|
+
|
|
850
|
+
for alias, alias_info in entry.get("host_aliases", {}).items():
|
|
851
|
+
if self.ssh_manager.has_host_alias(alias) and not force:
|
|
852
|
+
continue
|
|
853
|
+
self.ssh_manager.add_host_alias(
|
|
854
|
+
alias,
|
|
855
|
+
alias_info.get("hostname", "github.com"),
|
|
856
|
+
private_key,
|
|
857
|
+
alias_info.get("user", "git"),
|
|
858
|
+
)
|
|
859
|
+
|
|
860
|
+
imported += 1
|
|
861
|
+
echo_color("g", f'Imported account "{account_name}"')
|
|
862
|
+
|
|
863
|
+
echo_color("g", f"Import complete: {imported} imported, {skipped} skipped.")
|
|
864
|
+
return True
|
|
@@ -55,6 +55,8 @@ class CLI:
|
|
|
55
55
|
self._add_autocomplete_command(subparsers)
|
|
56
56
|
self._add_rule_command(subparsers)
|
|
57
57
|
self._add_alias_command(subparsers)
|
|
58
|
+
self._add_export_command(subparsers)
|
|
59
|
+
self._add_import_command(subparsers)
|
|
58
60
|
|
|
59
61
|
# Include account names in top-level completion so `gitacc <TAB>` works for
|
|
60
62
|
# the `gitacc <account>` shorthand, not just for subcommand names.
|
|
@@ -89,6 +91,8 @@ Examples:
|
|
|
89
91
|
gitacc rule hook bash Print the shell hook (add to ~/.bashrc)
|
|
90
92
|
gitacc alias add work github-work Bind a Host alias to an account's key
|
|
91
93
|
gitacc alias list List managed SSH config host aliases
|
|
94
|
+
gitacc export accounts.json Export account metadata to a file
|
|
95
|
+
gitacc import accounts.json Import accounts from an export file
|
|
92
96
|
"""
|
|
93
97
|
|
|
94
98
|
def _add_add_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
@@ -408,6 +412,50 @@ Examples:
|
|
|
408
412
|
)
|
|
409
413
|
list_parser.set_defaults(func=self._handle_alias_list)
|
|
410
414
|
|
|
415
|
+
def _add_export_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
416
|
+
"""Add the 'export' command parser."""
|
|
417
|
+
parser = subparsers.add_parser(
|
|
418
|
+
"export",
|
|
419
|
+
help="Export account metadata to a file",
|
|
420
|
+
description="Export registered accounts (metadata only by "
|
|
421
|
+
"default) to a portable file for backup or migration to "
|
|
422
|
+
"another machine",
|
|
423
|
+
)
|
|
424
|
+
parser.add_argument(
|
|
425
|
+
"output_file",
|
|
426
|
+
help="File to write the export to",
|
|
427
|
+
)
|
|
428
|
+
parser.add_argument(
|
|
429
|
+
"--include-keys",
|
|
430
|
+
dest="include_keys",
|
|
431
|
+
action="store_true",
|
|
432
|
+
help="Also embed each account's private/public SSH key file "
|
|
433
|
+
"contents in the export. The output file will contain private "
|
|
434
|
+
"key material in plain text (written with 0600 permissions) — "
|
|
435
|
+
"keep it secure.",
|
|
436
|
+
)
|
|
437
|
+
parser.set_defaults(func=self._handle_export)
|
|
438
|
+
|
|
439
|
+
def _add_import_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
440
|
+
"""Add the 'import' command parser."""
|
|
441
|
+
parser = subparsers.add_parser(
|
|
442
|
+
"import",
|
|
443
|
+
help="Import accounts from an export file",
|
|
444
|
+
description="Import accounts previously created with 'gitacc export'",
|
|
445
|
+
)
|
|
446
|
+
parser.add_argument(
|
|
447
|
+
"input_file",
|
|
448
|
+
help="File previously created with 'gitacc export'",
|
|
449
|
+
)
|
|
450
|
+
parser.add_argument(
|
|
451
|
+
"--force",
|
|
452
|
+
dest="force",
|
|
453
|
+
action="store_true",
|
|
454
|
+
help="Overwrite existing accounts, host aliases, and key files "
|
|
455
|
+
"with the same name/path",
|
|
456
|
+
)
|
|
457
|
+
parser.set_defaults(func=self._handle_import)
|
|
458
|
+
|
|
411
459
|
def _handle_autocomplete_install(self, args: argparse.Namespace) -> int:
|
|
412
460
|
"""Handle the 'autocomplete install' command."""
|
|
413
461
|
if not argcomplete:
|
|
@@ -746,6 +794,20 @@ Examples:
|
|
|
746
794
|
)
|
|
747
795
|
return 0
|
|
748
796
|
|
|
797
|
+
def _handle_export(self, args: argparse.Namespace) -> int:
|
|
798
|
+
"""Handle the 'export' command."""
|
|
799
|
+
success = self.account_manager.export_accounts(
|
|
800
|
+
args.output_file, include_keys=getattr(args, "include_keys", False)
|
|
801
|
+
)
|
|
802
|
+
return 0 if success else 1
|
|
803
|
+
|
|
804
|
+
def _handle_import(self, args: argparse.Namespace) -> int:
|
|
805
|
+
"""Handle the 'import' command."""
|
|
806
|
+
success = self.account_manager.import_accounts(
|
|
807
|
+
args.input_file, force=getattr(args, "force", False)
|
|
808
|
+
)
|
|
809
|
+
return 0 if success else 1
|
|
810
|
+
|
|
749
811
|
def _handle_account_name_shortcut(self, account_name: str) -> int:
|
|
750
812
|
"""Handle account name as shortcut for switch command."""
|
|
751
813
|
success = self.account_manager.switch_account(account_name)
|
|
@@ -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"
|
|
@@ -310,6 +310,26 @@ class SSHManager:
|
|
|
310
310
|
except Exception:
|
|
311
311
|
return None
|
|
312
312
|
|
|
313
|
+
def get_private_key_content(self, private_key_path: str) -> Optional[str]:
|
|
314
|
+
"""Read private key file content.
|
|
315
|
+
|
|
316
|
+
Used by `gitacc export --include-keys` to embed key material in an
|
|
317
|
+
export file; callers must handle the returned content as sensitive
|
|
318
|
+
(e.g. write it only to a restrictively-permissioned file).
|
|
319
|
+
|
|
320
|
+
Args:
|
|
321
|
+
private_key_path: Path to private key file
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
Private key content (unmodified, including trailing newline) or
|
|
325
|
+
None on error
|
|
326
|
+
"""
|
|
327
|
+
try:
|
|
328
|
+
with open(private_key_path, "r") as f:
|
|
329
|
+
return f.read()
|
|
330
|
+
except Exception:
|
|
331
|
+
return None
|
|
332
|
+
|
|
313
333
|
def get_ssh_config_path(self) -> Path:
|
|
314
334
|
"""Get the path to the user's SSH config file (~/.ssh/config)."""
|
|
315
335
|
return self.ssh_dir / "config"
|
|
@@ -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",
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"""Tests for AccountManager — orchestration of account operations."""
|
|
2
2
|
|
|
3
|
+
import json
|
|
3
4
|
import pytest
|
|
4
5
|
from unittest.mock import patch, MagicMock
|
|
5
6
|
from gitacc_switcher.account_manager import AccountManager
|
|
7
|
+
from gitacc_switcher.config_manager import ConfigManager
|
|
8
|
+
from gitacc_switcher.ssh_manager import SSHManager
|
|
6
9
|
|
|
7
10
|
|
|
8
11
|
@pytest.fixture
|
|
@@ -533,6 +536,67 @@ class TestGetStatus:
|
|
|
533
536
|
assert status["matched_key_account"] is None
|
|
534
537
|
|
|
535
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
|
+
|
|
536
600
|
class TestRunDoctor:
|
|
537
601
|
def test_agent_running_and_all_keys_ok(self, manager, tmp_path):
|
|
538
602
|
key = tmp_path / "id_rsa_work"
|
|
@@ -653,3 +717,301 @@ class TestListHostAliases:
|
|
|
653
717
|
def test_empty(self, manager):
|
|
654
718
|
manager.ssh_manager.list_host_aliases.return_value = {}
|
|
655
719
|
assert manager.list_host_aliases() == {}
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
class TestExportAccounts:
|
|
723
|
+
def test_fails_when_no_accounts(self, manager, tmp_path):
|
|
724
|
+
manager.config_manager.read_accounts.return_value = {}
|
|
725
|
+
assert manager.export_accounts(tmp_path / "out.json") is False
|
|
726
|
+
|
|
727
|
+
def test_writes_metadata_only_by_default(self, manager, tmp_path):
|
|
728
|
+
manager.config_manager.read_accounts.return_value = {"work": WORK_ACCOUNT}
|
|
729
|
+
manager.ssh_manager.list_host_aliases.return_value = {}
|
|
730
|
+
output = tmp_path / "out.json"
|
|
731
|
+
|
|
732
|
+
assert manager.export_accounts(output) is True
|
|
733
|
+
|
|
734
|
+
data = json.loads(output.read_text())
|
|
735
|
+
assert data["include_keys"] is False
|
|
736
|
+
work = data["accounts"]["work"]
|
|
737
|
+
assert work["name"] == "Work User"
|
|
738
|
+
assert work["email"] == "work@company.com"
|
|
739
|
+
assert work["private_key"] == WORK_ACCOUNT["private_key"]
|
|
740
|
+
assert "private_key_content" not in work
|
|
741
|
+
assert "public_key_content" not in work
|
|
742
|
+
manager.ssh_manager.get_private_key_content.assert_not_called()
|
|
743
|
+
|
|
744
|
+
def test_includes_key_content_when_opted_in(self, manager, tmp_path):
|
|
745
|
+
manager.config_manager.read_accounts.return_value = {"work": WORK_ACCOUNT}
|
|
746
|
+
manager.ssh_manager.list_host_aliases.return_value = {}
|
|
747
|
+
manager.ssh_manager.get_public_key_content.return_value = "ssh-rsa AAAA"
|
|
748
|
+
manager.ssh_manager.get_private_key_content.return_value = "PRIVATE-DATA"
|
|
749
|
+
output = tmp_path / "out.json"
|
|
750
|
+
|
|
751
|
+
assert manager.export_accounts(output, include_keys=True) is True
|
|
752
|
+
|
|
753
|
+
data = json.loads(output.read_text())
|
|
754
|
+
work = data["accounts"]["work"]
|
|
755
|
+
assert work["private_key_content"] == "PRIVATE-DATA"
|
|
756
|
+
assert work["public_key_content"] == "ssh-rsa AAAA"
|
|
757
|
+
assert (output.stat().st_mode & 0o777) == 0o600
|
|
758
|
+
|
|
759
|
+
def test_includes_host_aliases_bound_to_account_key(self, manager, tmp_path):
|
|
760
|
+
manager.config_manager.read_accounts.return_value = {"work": WORK_ACCOUNT}
|
|
761
|
+
manager.ssh_manager.list_host_aliases.return_value = {
|
|
762
|
+
"github-work": {
|
|
763
|
+
"hostname": "github.com",
|
|
764
|
+
"user": "git",
|
|
765
|
+
"identity_file": WORK_ACCOUNT["private_key"],
|
|
766
|
+
},
|
|
767
|
+
"other-alias": {
|
|
768
|
+
"hostname": "gitlab.com",
|
|
769
|
+
"user": "git",
|
|
770
|
+
"identity_file": "/home/user/.ssh/id_rsa_other",
|
|
771
|
+
},
|
|
772
|
+
}
|
|
773
|
+
output = tmp_path / "out.json"
|
|
774
|
+
|
|
775
|
+
assert manager.export_accounts(output) is True
|
|
776
|
+
|
|
777
|
+
work = json.loads(output.read_text())["accounts"]["work"]
|
|
778
|
+
assert work["host_aliases"] == {
|
|
779
|
+
"github-work": {"hostname": "github.com", "user": "git"}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
class TestImportAccounts:
|
|
784
|
+
def _write_export(self, tmp_path, accounts, include_keys=False):
|
|
785
|
+
export_file = tmp_path / "export.json"
|
|
786
|
+
export_file.write_text(
|
|
787
|
+
json.dumps(
|
|
788
|
+
{"version": 1, "include_keys": include_keys, "accounts": accounts}
|
|
789
|
+
)
|
|
790
|
+
)
|
|
791
|
+
return export_file
|
|
792
|
+
|
|
793
|
+
def test_fails_when_file_missing(self, manager, tmp_path):
|
|
794
|
+
assert manager.import_accounts(tmp_path / "nope.json") is False
|
|
795
|
+
|
|
796
|
+
def test_fails_when_no_accounts_in_file(self, manager, tmp_path):
|
|
797
|
+
export_file = self._write_export(tmp_path, {})
|
|
798
|
+
assert manager.import_accounts(export_file) is False
|
|
799
|
+
|
|
800
|
+
def test_imports_new_account(self, manager, tmp_path):
|
|
801
|
+
export_file = self._write_export(
|
|
802
|
+
tmp_path,
|
|
803
|
+
{
|
|
804
|
+
"work": {
|
|
805
|
+
"name": "Work User",
|
|
806
|
+
"email": "work@company.com",
|
|
807
|
+
"sign": "false",
|
|
808
|
+
"private_key": "/home/user/.ssh/id_rsa_work",
|
|
809
|
+
"public_key": "/home/user/.ssh/id_rsa_work.pub",
|
|
810
|
+
}
|
|
811
|
+
},
|
|
812
|
+
)
|
|
813
|
+
manager.config_manager.get_account.return_value = None
|
|
814
|
+
manager.config_manager.add_account.return_value = True
|
|
815
|
+
|
|
816
|
+
assert manager.import_accounts(export_file) is True
|
|
817
|
+
|
|
818
|
+
manager.config_manager.add_account.assert_called_once_with(
|
|
819
|
+
"work",
|
|
820
|
+
"Work User",
|
|
821
|
+
"work@company.com",
|
|
822
|
+
"/home/user/.ssh/id_rsa_work",
|
|
823
|
+
"/home/user/.ssh/id_rsa_work.pub",
|
|
824
|
+
sign=False,
|
|
825
|
+
)
|
|
826
|
+
manager.config_manager.remove_account.assert_not_called()
|
|
827
|
+
|
|
828
|
+
def test_skips_existing_account_without_force(self, manager, tmp_path):
|
|
829
|
+
export_file = self._write_export(
|
|
830
|
+
tmp_path,
|
|
831
|
+
{
|
|
832
|
+
"work": {
|
|
833
|
+
"name": "Work User",
|
|
834
|
+
"email": "work@company.com",
|
|
835
|
+
"private_key": "/home/user/.ssh/id_rsa_work",
|
|
836
|
+
"public_key": "/home/user/.ssh/id_rsa_work.pub",
|
|
837
|
+
}
|
|
838
|
+
},
|
|
839
|
+
)
|
|
840
|
+
manager.config_manager.get_account.return_value = WORK_ACCOUNT
|
|
841
|
+
|
|
842
|
+
assert manager.import_accounts(export_file) is True
|
|
843
|
+
|
|
844
|
+
manager.config_manager.add_account.assert_not_called()
|
|
845
|
+
|
|
846
|
+
def test_force_overwrites_existing_account(self, manager, tmp_path):
|
|
847
|
+
export_file = self._write_export(
|
|
848
|
+
tmp_path,
|
|
849
|
+
{
|
|
850
|
+
"work": {
|
|
851
|
+
"name": "New Name",
|
|
852
|
+
"email": "work@company.com",
|
|
853
|
+
"private_key": "/home/user/.ssh/id_rsa_work",
|
|
854
|
+
"public_key": "/home/user/.ssh/id_rsa_work.pub",
|
|
855
|
+
}
|
|
856
|
+
},
|
|
857
|
+
)
|
|
858
|
+
manager.config_manager.get_account.return_value = WORK_ACCOUNT
|
|
859
|
+
manager.config_manager.add_account.return_value = True
|
|
860
|
+
|
|
861
|
+
assert manager.import_accounts(export_file, force=True) is True
|
|
862
|
+
|
|
863
|
+
manager.config_manager.remove_account.assert_called_once_with("work")
|
|
864
|
+
manager.config_manager.add_account.assert_called_once()
|
|
865
|
+
|
|
866
|
+
def test_writes_key_material_when_present(self, manager, tmp_path):
|
|
867
|
+
private_key = tmp_path / "id_rsa_work"
|
|
868
|
+
public_key = tmp_path / "id_rsa_work.pub"
|
|
869
|
+
export_file = self._write_export(
|
|
870
|
+
tmp_path,
|
|
871
|
+
{
|
|
872
|
+
"work": {
|
|
873
|
+
"name": "Work User",
|
|
874
|
+
"email": "work@company.com",
|
|
875
|
+
"private_key": str(private_key),
|
|
876
|
+
"public_key": str(public_key),
|
|
877
|
+
"private_key_content": "PRIVATE-DATA",
|
|
878
|
+
"public_key_content": "ssh-rsa AAAA",
|
|
879
|
+
}
|
|
880
|
+
},
|
|
881
|
+
include_keys=True,
|
|
882
|
+
)
|
|
883
|
+
manager.config_manager.get_account.return_value = None
|
|
884
|
+
manager.config_manager.add_account.return_value = True
|
|
885
|
+
|
|
886
|
+
assert manager.import_accounts(export_file) is True
|
|
887
|
+
|
|
888
|
+
assert private_key.read_text() == "PRIVATE-DATA"
|
|
889
|
+
assert public_key.read_text() == "ssh-rsa AAAA"
|
|
890
|
+
assert (private_key.stat().st_mode & 0o777) == 0o600
|
|
891
|
+
|
|
892
|
+
def test_does_not_overwrite_existing_key_file_without_force(
|
|
893
|
+
self, manager, tmp_path
|
|
894
|
+
):
|
|
895
|
+
private_key = tmp_path / "id_rsa_work"
|
|
896
|
+
private_key.write_text("ORIGINAL")
|
|
897
|
+
export_file = self._write_export(
|
|
898
|
+
tmp_path,
|
|
899
|
+
{
|
|
900
|
+
"work": {
|
|
901
|
+
"name": "Work User",
|
|
902
|
+
"email": "work@company.com",
|
|
903
|
+
"private_key": str(private_key),
|
|
904
|
+
"public_key": str(tmp_path / "id_rsa_work.pub"),
|
|
905
|
+
"private_key_content": "IMPORTED-DATA",
|
|
906
|
+
}
|
|
907
|
+
},
|
|
908
|
+
include_keys=True,
|
|
909
|
+
)
|
|
910
|
+
manager.config_manager.get_account.return_value = None
|
|
911
|
+
manager.config_manager.add_account.return_value = True
|
|
912
|
+
|
|
913
|
+
manager.import_accounts(export_file)
|
|
914
|
+
|
|
915
|
+
assert private_key.read_text() == "ORIGINAL"
|
|
916
|
+
|
|
917
|
+
def test_imports_host_aliases(self, manager, tmp_path):
|
|
918
|
+
export_file = self._write_export(
|
|
919
|
+
tmp_path,
|
|
920
|
+
{
|
|
921
|
+
"work": {
|
|
922
|
+
"name": "Work User",
|
|
923
|
+
"email": "work@company.com",
|
|
924
|
+
"private_key": "/home/user/.ssh/id_rsa_work",
|
|
925
|
+
"public_key": "/home/user/.ssh/id_rsa_work.pub",
|
|
926
|
+
"host_aliases": {
|
|
927
|
+
"github-work": {"hostname": "github.com", "user": "git"}
|
|
928
|
+
},
|
|
929
|
+
}
|
|
930
|
+
},
|
|
931
|
+
)
|
|
932
|
+
manager.config_manager.get_account.return_value = None
|
|
933
|
+
manager.config_manager.add_account.return_value = True
|
|
934
|
+
manager.ssh_manager.has_host_alias.return_value = False
|
|
935
|
+
|
|
936
|
+
manager.import_accounts(export_file)
|
|
937
|
+
|
|
938
|
+
manager.ssh_manager.add_host_alias.assert_called_once_with(
|
|
939
|
+
"github-work",
|
|
940
|
+
"github.com",
|
|
941
|
+
"/home/user/.ssh/id_rsa_work",
|
|
942
|
+
"git",
|
|
943
|
+
)
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
class TestExportImportRoundTrip:
|
|
947
|
+
"""Round-trips a real (unmocked) config through export then import."""
|
|
948
|
+
|
|
949
|
+
def _real_manager(self, tmp_path, label):
|
|
950
|
+
config = ConfigManager.__new__(ConfigManager)
|
|
951
|
+
config.gitacc_file = tmp_path / f"{label}.gitacc"
|
|
952
|
+
config._ensure_gitacc_file()
|
|
953
|
+
|
|
954
|
+
ssh = SSHManager.__new__(SSHManager)
|
|
955
|
+
ssh.ssh_dir = tmp_path / f"{label}-ssh"
|
|
956
|
+
ssh.ssh_dir.mkdir(mode=0o700)
|
|
957
|
+
|
|
958
|
+
mgr = AccountManager.__new__(AccountManager)
|
|
959
|
+
mgr.config_manager = config
|
|
960
|
+
mgr.ssh_manager = ssh
|
|
961
|
+
return mgr
|
|
962
|
+
|
|
963
|
+
def test_round_trip_metadata_only(self, tmp_path):
|
|
964
|
+
source = self._real_manager(tmp_path, "source")
|
|
965
|
+
key = source.ssh_manager.ssh_dir / "id_rsa_work"
|
|
966
|
+
key.write_text("PRIVATE")
|
|
967
|
+
pub = source.ssh_manager.ssh_dir / "id_rsa_work.pub"
|
|
968
|
+
pub.write_text("PUBLIC")
|
|
969
|
+
assert source.config_manager.add_account(
|
|
970
|
+
"work", "Work User", "work@company.com", str(key), str(pub), sign=False
|
|
971
|
+
)
|
|
972
|
+
|
|
973
|
+
export_file = tmp_path / "export.json"
|
|
974
|
+
assert source.export_accounts(export_file) is True
|
|
975
|
+
|
|
976
|
+
target = self._real_manager(tmp_path, "target")
|
|
977
|
+
assert target.import_accounts(export_file) is True
|
|
978
|
+
|
|
979
|
+
imported = target.config_manager.read_accounts()
|
|
980
|
+
assert imported["work"]["name"] == "Work User"
|
|
981
|
+
assert imported["work"]["email"] == "work@company.com"
|
|
982
|
+
assert imported["work"]["private_key"] == str(key)
|
|
983
|
+
# Metadata-only: no key material was embedded, so nothing is written
|
|
984
|
+
# to disk at the target other than the .gitacc entry itself.
|
|
985
|
+
assert not (target.ssh_manager.ssh_dir / "id_rsa_work").exists()
|
|
986
|
+
|
|
987
|
+
def test_round_trip_with_keys_restores_key_files(self, tmp_path):
|
|
988
|
+
source = self._real_manager(tmp_path, "shared")
|
|
989
|
+
key = source.ssh_manager.ssh_dir / "id_rsa_work"
|
|
990
|
+
key.write_text("PRIVATE-CONTENT")
|
|
991
|
+
pub = source.ssh_manager.ssh_dir / "id_rsa_work.pub"
|
|
992
|
+
pub.write_text("PUBLIC-CONTENT")
|
|
993
|
+
assert source.config_manager.add_account(
|
|
994
|
+
"work", "Work User", "work@company.com", str(key), str(pub), sign=False
|
|
995
|
+
)
|
|
996
|
+
|
|
997
|
+
export_file = tmp_path / "export.json"
|
|
998
|
+
assert source.export_accounts(export_file, include_keys=True) is True
|
|
999
|
+
|
|
1000
|
+
# Simulate a fresh machine: a separate .gitacc, and the key files
|
|
1001
|
+
# for this account are gone (never existed there).
|
|
1002
|
+
key.unlink()
|
|
1003
|
+
pub.unlink()
|
|
1004
|
+
target = AccountManager.__new__(AccountManager)
|
|
1005
|
+
target_config = ConfigManager.__new__(ConfigManager)
|
|
1006
|
+
target_config.gitacc_file = tmp_path / "fresh.gitacc"
|
|
1007
|
+
target_config._ensure_gitacc_file()
|
|
1008
|
+
target.config_manager = target_config
|
|
1009
|
+
target.ssh_manager = source.ssh_manager # same ~/.ssh path on "this machine"
|
|
1010
|
+
|
|
1011
|
+
assert target.import_accounts(export_file) is True
|
|
1012
|
+
|
|
1013
|
+
assert key.read_text() == "PRIVATE-CONTENT"
|
|
1014
|
+
assert pub.read_text() == "PUBLIC-CONTENT"
|
|
1015
|
+
assert (key.stat().st_mode & 0o777) == 0o600
|
|
1016
|
+
imported = target_config.read_accounts()
|
|
1017
|
+
assert imported["work"]["private_key"] == str(key)
|
|
@@ -674,3 +674,64 @@ class TestAliasCommandHandlers:
|
|
|
674
674
|
out = capsys.readouterr().out
|
|
675
675
|
assert "github-work" in out
|
|
676
676
|
assert "git@github.com" in out
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
# ---------------------------------------------------------------------------
|
|
680
|
+
# `export` / `import` commands
|
|
681
|
+
# ---------------------------------------------------------------------------
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
class TestExportImportParserCommands:
|
|
685
|
+
def test_export_parses_output_file(self, cli):
|
|
686
|
+
args = cli.parser.parse_args(["export", "accounts.json"])
|
|
687
|
+
assert args.command == "export"
|
|
688
|
+
assert args.output_file == "accounts.json"
|
|
689
|
+
assert args.include_keys is False
|
|
690
|
+
|
|
691
|
+
def test_export_include_keys_flag(self, cli):
|
|
692
|
+
args = cli.parser.parse_args(["export", "accounts.json", "--include-keys"])
|
|
693
|
+
assert args.include_keys is True
|
|
694
|
+
|
|
695
|
+
def test_import_parses_input_file(self, cli):
|
|
696
|
+
args = cli.parser.parse_args(["import", "accounts.json"])
|
|
697
|
+
assert args.command == "import"
|
|
698
|
+
assert args.input_file == "accounts.json"
|
|
699
|
+
assert args.force is False
|
|
700
|
+
|
|
701
|
+
def test_import_force_flag(self, cli):
|
|
702
|
+
args = cli.parser.parse_args(["import", "accounts.json", "--force"])
|
|
703
|
+
assert args.force is True
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
class TestExportImportHandlers:
|
|
707
|
+
def test_handle_export_success(self, cli):
|
|
708
|
+
args = argparse.Namespace(output_file="accounts.json", include_keys=False)
|
|
709
|
+
cli.account_manager.export_accounts.return_value = True
|
|
710
|
+
assert cli._handle_export(args) == 0
|
|
711
|
+
cli.account_manager.export_accounts.assert_called_once_with(
|
|
712
|
+
"accounts.json", include_keys=False
|
|
713
|
+
)
|
|
714
|
+
|
|
715
|
+
def test_handle_export_failure(self, cli):
|
|
716
|
+
args = argparse.Namespace(output_file="accounts.json", include_keys=True)
|
|
717
|
+
cli.account_manager.export_accounts.return_value = False
|
|
718
|
+
assert cli._handle_export(args) == 1
|
|
719
|
+
cli.account_manager.export_accounts.assert_called_once_with(
|
|
720
|
+
"accounts.json", include_keys=True
|
|
721
|
+
)
|
|
722
|
+
|
|
723
|
+
def test_handle_import_success(self, cli):
|
|
724
|
+
args = argparse.Namespace(input_file="accounts.json", force=False)
|
|
725
|
+
cli.account_manager.import_accounts.return_value = True
|
|
726
|
+
assert cli._handle_import(args) == 0
|
|
727
|
+
cli.account_manager.import_accounts.assert_called_once_with(
|
|
728
|
+
"accounts.json", force=False
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
def test_handle_import_failure(self, cli):
|
|
732
|
+
args = argparse.Namespace(input_file="accounts.json", force=True)
|
|
733
|
+
cli.account_manager.import_accounts.return_value = False
|
|
734
|
+
assert cli._handle_import(args) == 1
|
|
735
|
+
cli.account_manager.import_accounts.assert_called_once_with(
|
|
736
|
+
"accounts.json", force=True
|
|
737
|
+
)
|
|
@@ -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"
|
|
@@ -185,6 +185,20 @@ class TestGetPublicKeyContent:
|
|
|
185
185
|
assert ssh.get_public_key_content(str(tmp_path / "nonexistent.pub")) is None
|
|
186
186
|
|
|
187
187
|
|
|
188
|
+
class TestGetPrivateKeyContent:
|
|
189
|
+
def test_reads_content(self, ssh, tmp_path):
|
|
190
|
+
priv_key = tmp_path / "id_rsa_test"
|
|
191
|
+
priv_key.write_text(
|
|
192
|
+
"-----BEGIN OPENSSH PRIVATE KEY-----\nabc\n-----END OPENSSH PRIVATE KEY-----\n"
|
|
193
|
+
)
|
|
194
|
+
assert ssh.get_private_key_content(str(priv_key)) == (
|
|
195
|
+
"-----BEGIN OPENSSH PRIVATE KEY-----\nabc\n-----END OPENSSH PRIVATE KEY-----\n"
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def test_returns_none_for_missing_file(self, ssh, tmp_path):
|
|
199
|
+
assert ssh.get_private_key_content(str(tmp_path / "nonexistent")) is None
|
|
200
|
+
|
|
201
|
+
|
|
188
202
|
class TestGetAgentFingerprints:
|
|
189
203
|
def test_returns_fingerprints_for_loaded_keys(self, ssh):
|
|
190
204
|
output = (
|
|
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.6.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
|