feat: add git push subcommand

This commit is contained in:
cătălin 2024-09-04 18:57:27 +02:00
commit 8076807f22
No known key found for this signature in database
8 changed files with 92 additions and 30 deletions

View file

@ -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 notebooks # list current notebooks
halig git commit
halig git pull
halig git push
```

View file

@ -1 +1 @@
__version__ = "0.5.1"
__version__ = "0.5.1a2"

View 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)

View file

@ -1,32 +1,7 @@
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
from halig.commands.git.base import GitBaseCommand
class GitCommitCommand(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)
class GitCommitCommand(GitBaseCommand):
def run(self):
"""Add all .age files to git and commit them using gitpython"""
self.repo.index.add(

View 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()

View file

@ -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
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"""
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
OPTION_CONFIG_HELP = "Configuration file. Must be YAML and schema compatible"

View file

@ -10,6 +10,7 @@ from halig import literals
from halig.__version__ import __version__
from halig.commands.edit import EditCommand
from halig.commands.git.commit import GitCommitCommand
from halig.commands.git.push import GitPushCommand
from halig.commands.import_unencrypted import ImportCommand
from halig.commands.notebooks import NotebooksCommand
from halig.commands.reencrypt import ReencryptCommand
@ -165,6 +166,15 @@ def git_commit(
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)
@capture
def version():

View 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"])