feat: add git push subcommand
This commit is contained in:
parent
c859c60c8d
commit
8076807f22
8 changed files with 92 additions and 30 deletions
|
|
@ -51,6 +51,5 @@ halig edit some_notebook # edit today's note relative to <notebooks_root_pat
|
||||||
halig edit some_notebook/foo # edit <notebooks_root_path>/some_notebook/foo.age
|
halig edit some_notebook/foo # edit <notebooks_root_path>/some_notebook/foo.age
|
||||||
halig notebooks # list current notebooks
|
halig notebooks # list current notebooks
|
||||||
halig git commit
|
halig git commit
|
||||||
halig git pull
|
|
||||||
halig git push
|
halig git push
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
__version__ = "0.5.1"
|
__version__ = "0.5.1a2"
|
||||||
|
|
|
||||||
28
halig/commands/git/base.py
Normal file
28
halig/commands/git/base.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from git import Repo
|
||||||
|
from rich import print
|
||||||
|
|
||||||
|
from halig.commands.base import BaseCommand
|
||||||
|
from halig.encryption import Encryptor
|
||||||
|
from halig.settings import Settings
|
||||||
|
|
||||||
|
|
||||||
|
class GitBaseCommand(BaseCommand):
|
||||||
|
@staticmethod
|
||||||
|
def __init_repo(repo_path: Path) -> Repo:
|
||||||
|
"""Check if `repo_path` is a git repo. If not, initialize it"""
|
||||||
|
|
||||||
|
if not (repo_path / ".git").is_dir():
|
||||||
|
print(f"[yellow] {repo_path} is not a git repo, initializing ...")
|
||||||
|
Repo.init(repo_path)
|
||||||
|
return Repo(repo_path)
|
||||||
|
|
||||||
|
return Repo(repo_path)
|
||||||
|
|
||||||
|
def __init__(self, settings: Settings, message: str | None = None):
|
||||||
|
super().__init__(settings)
|
||||||
|
self.settings = settings
|
||||||
|
self.encryptor = Encryptor(self.settings)
|
||||||
|
self.message = message or self.settings.default_commit_message
|
||||||
|
self.repo = self.__init_repo(self.settings.notebooks_root_path)
|
||||||
|
|
@ -1,32 +1,7 @@
|
||||||
from pathlib import Path
|
from halig.commands.git.base import GitBaseCommand
|
||||||
|
|
||||||
from git import Repo
|
|
||||||
from rich import print
|
|
||||||
|
|
||||||
from halig.commands.base import BaseCommand
|
|
||||||
from halig.encryption import Encryptor
|
|
||||||
from halig.settings import Settings
|
|
||||||
|
|
||||||
|
|
||||||
class GitCommitCommand(BaseCommand):
|
class GitCommitCommand(GitBaseCommand):
|
||||||
@staticmethod
|
|
||||||
def __init_repo(repo_path: Path) -> Repo:
|
|
||||||
"""Check if `repo_path` is a git repo. If not, initialize it"""
|
|
||||||
|
|
||||||
if not (repo_path / ".git").is_dir():
|
|
||||||
print(f"[yellow] {repo_path} is not a git repo, initializing ...")
|
|
||||||
Repo.init(repo_path)
|
|
||||||
return Repo(repo_path)
|
|
||||||
|
|
||||||
return Repo(repo_path)
|
|
||||||
|
|
||||||
def __init__(self, settings: Settings, message: str | None = None):
|
|
||||||
super().__init__(settings)
|
|
||||||
self.settings = settings
|
|
||||||
self.encryptor = Encryptor(self.settings)
|
|
||||||
self.message = message or self.settings.default_commit_message
|
|
||||||
self.repo = self.__init_repo(self.settings.notebooks_root_path)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Add all .age files to git and commit them using gitpython"""
|
"""Add all .age files to git and commit them using gitpython"""
|
||||||
self.repo.index.add(
|
self.repo.index.add(
|
||||||
|
|
|
||||||
12
halig/commands/git/push.py
Normal file
12
halig/commands/git/push.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from halig.commands.git.base import GitBaseCommand
|
||||||
|
|
||||||
|
|
||||||
|
class GitPushCommand(GitBaseCommand):
|
||||||
|
def run(self, remotes: list[str] | None = None):
|
||||||
|
"""Push all changes to the remote git repo"""
|
||||||
|
if not remotes:
|
||||||
|
self.repo.remotes.origin.push()
|
||||||
|
return
|
||||||
|
|
||||||
|
for remote in remotes:
|
||||||
|
self.repo.remotes[remote].push()
|
||||||
|
|
@ -12,7 +12,8 @@ which are indexed into a SQLite FTS5 database located at `~/.cache/halig/halig.d
|
||||||
COMMANDS_REENCRYPT_HELP = """Reencrypt all available notes. This operation is useful
|
COMMANDS_REENCRYPT_HELP = """Reencrypt all available notes. This operation is useful
|
||||||
when new public keys have been added to the config file and you want the notes
|
when new public keys have been added to the config file and you want the notes
|
||||||
to be seen by the new pairing private keys"""
|
to be seen by the new pairing private keys"""
|
||||||
COMMANDS_GIT_COMMIT_HELP = """Commit all .age files to git"""
|
COMMANDS_GIT_COMMIT_HELP = "Commit all .age files to git"
|
||||||
|
COMMANDS_GIT_PUSH_HELP = "Push all .age files to git"
|
||||||
|
|
||||||
# OPTIONS
|
# OPTIONS
|
||||||
OPTION_CONFIG_HELP = "Configuration file. Must be YAML and schema compatible"
|
OPTION_CONFIG_HELP = "Configuration file. Must be YAML and schema compatible"
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ from halig import literals
|
||||||
from halig.__version__ import __version__
|
from halig.__version__ import __version__
|
||||||
from halig.commands.edit import EditCommand
|
from halig.commands.edit import EditCommand
|
||||||
from halig.commands.git.commit import GitCommitCommand
|
from halig.commands.git.commit import GitCommitCommand
|
||||||
|
from halig.commands.git.push import GitPushCommand
|
||||||
from halig.commands.import_unencrypted import ImportCommand
|
from halig.commands.import_unencrypted import ImportCommand
|
||||||
from halig.commands.notebooks import NotebooksCommand
|
from halig.commands.notebooks import NotebooksCommand
|
||||||
from halig.commands.reencrypt import ReencryptCommand
|
from halig.commands.reencrypt import ReencryptCommand
|
||||||
|
|
@ -165,6 +166,15 @@ def git_commit(
|
||||||
command.run()
|
command.run()
|
||||||
|
|
||||||
|
|
||||||
|
@git_app.command(name="push", help=literals.COMMANDS_GIT_PUSH_HELP)
|
||||||
|
def git_push(
|
||||||
|
config: Path | None = config_option,
|
||||||
|
):
|
||||||
|
settings = load_from_file(config)
|
||||||
|
command = GitPushCommand(settings=settings)
|
||||||
|
command.run()
|
||||||
|
|
||||||
|
|
||||||
@app.command(help=literals.COMMANDS_VERSION)
|
@app.command(help=literals.COMMANDS_VERSION)
|
||||||
@capture
|
@capture
|
||||||
def version():
|
def version():
|
||||||
|
|
|
||||||
37
tests/commands/test_git/test_push.py
Normal file
37
tests/commands/test_git/test_push.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from halig.commands.git.commit import GitCommitCommand
|
||||||
|
from halig.commands.git.push import GitPushCommand
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def command(settings, faker):
|
||||||
|
"""Configure a local remote for testing"""
|
||||||
|
commit_command = GitCommitCommand(settings)
|
||||||
|
new_path = shutil.copytree(settings.notebooks_root_path , settings.notebooks_root_path / "../remote")
|
||||||
|
new_path = new_path.resolve()
|
||||||
|
for _ in range(10):
|
||||||
|
random_age_file = settings.notebooks_root_path / f"{faker.word()}.age"
|
||||||
|
random_age_file.touch()
|
||||||
|
commit_command.run()
|
||||||
|
|
||||||
|
push_command = GitPushCommand(settings)
|
||||||
|
|
||||||
|
push_command.repo.create_remote("origin", str(new_path))
|
||||||
|
|
||||||
|
return push_command
|
||||||
|
|
||||||
|
|
||||||
|
def test_push_to_origin(settings, command):
|
||||||
|
"""Test that the command pushes to the origin remote"""
|
||||||
|
command.run()
|
||||||
|
|
||||||
|
def test_push_to_custom_remote(settings, command):
|
||||||
|
"""Test that the command pushes to a custom remote"""
|
||||||
|
|
||||||
|
remote_path = settings.notebooks_root_path / "../remote"
|
||||||
|
command.repo.create_remote("custom", str(remote_path.resolve()))
|
||||||
|
|
||||||
|
command.run(remotes=["custom"])
|
||||||
Loading…
Add table
Add a link
Reference in a new issue