mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-17 09:23:40 -05:00
fix(application): exit after committing changes
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import questionary
|
import questionary
|
||||||
import typer
|
|
||||||
from rich import print
|
from rich import print
|
||||||
|
|
||||||
from obsidian_metadata._config import Config
|
from obsidian_metadata._config import Config
|
||||||
@@ -132,8 +131,8 @@ class Application:
|
|||||||
if operation == "review_changes":
|
if operation == "review_changes":
|
||||||
self.review_changes()
|
self.review_changes()
|
||||||
|
|
||||||
if operation == "commit_changes":
|
if operation == "commit_changes" and self.commit_changes():
|
||||||
self.commit_changes()
|
break
|
||||||
|
|
||||||
if operation == "abort":
|
if operation == "abort":
|
||||||
break
|
break
|
||||||
@@ -346,25 +345,29 @@ class Application:
|
|||||||
break
|
break
|
||||||
changed_notes[note_to_review].print_diff()
|
changed_notes[note_to_review].print_diff()
|
||||||
|
|
||||||
def commit_changes(self) -> None:
|
def commit_changes(self) -> bool:
|
||||||
"""Write all changes to disk."""
|
"""Write all changes to disk.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if changes were committed, False otherwise.
|
||||||
|
"""
|
||||||
changed_notes = self.vault.get_changed_notes()
|
changed_notes = self.vault.get_changed_notes()
|
||||||
|
|
||||||
if len(changed_notes) == 0:
|
if len(changed_notes) == 0:
|
||||||
print("\n")
|
print("\n")
|
||||||
alerts.notice("No changes to commit.\n")
|
alerts.notice("No changes to commit.\n")
|
||||||
return
|
return False
|
||||||
|
|
||||||
backup = questionary.confirm("Create backup before committing changes").ask()
|
backup = questionary.confirm("Create backup before committing changes").ask()
|
||||||
if backup is None:
|
if backup is None:
|
||||||
return
|
return False
|
||||||
if backup:
|
if backup:
|
||||||
self.vault.backup()
|
self.vault.backup()
|
||||||
|
|
||||||
if questionary.confirm(f"Commit {len(changed_notes)} changed files to disk?").ask():
|
if questionary.confirm(f"Commit {len(changed_notes)} changed files to disk?").ask():
|
||||||
|
|
||||||
self.vault.write()
|
self.vault.write()
|
||||||
alerts.success("Changes committed to disk. Exiting.")
|
alerts.success(f"{len(changed_notes)} changes committed to disk. Exiting")
|
||||||
typer.Exit()
|
return True
|
||||||
|
|
||||||
return
|
return False
|
||||||
|
|||||||
@@ -350,11 +350,14 @@ class Note:
|
|||||||
|
|
||||||
self.sub(current_frontmatter, new_frontmatter)
|
self.sub(current_frontmatter, new_frontmatter)
|
||||||
|
|
||||||
def write(self, path: Path | None = None) -> None:
|
def write(self, path: Path = None) -> None:
|
||||||
"""Writes the note's content to disk.
|
"""Writes the note's content to disk.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
path (Path): Path to write the note to. Defaults to the note's path.
|
path (Path): Path to write the note to. Defaults to the note's path.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
typer.Exit: If the note's path is not found.
|
||||||
"""
|
"""
|
||||||
p = self.note_path if path is None else path
|
p = self.note_path if path is None else path
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ class Vault:
|
|||||||
vault (Path): Path to the vault.
|
vault (Path): Path to the vault.
|
||||||
dry_run (bool): Whether to perform a dry run.
|
dry_run (bool): Whether to perform a dry run.
|
||||||
backup_path (Path): Path to the backup of the vault.
|
backup_path (Path): Path to the backup of the vault.
|
||||||
new_vault (Path): Path to a new vault.
|
|
||||||
notes (list[Note]): List of all notes in the vault.
|
notes (list[Note]): List of all notes in the vault.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -32,7 +31,6 @@ class Vault:
|
|||||||
self.vault_path: Path = config.vault_path
|
self.vault_path: Path = config.vault_path
|
||||||
self.dry_run: bool = dry_run
|
self.dry_run: bool = dry_run
|
||||||
self.backup_path: Path = self.vault_path.parent / f"{self.vault_path.name}.bak"
|
self.backup_path: Path = self.vault_path.parent / f"{self.vault_path.name}.bak"
|
||||||
self.new_vault_path: Path = self.vault_path.parent / f"{self.vault_path.name}.new"
|
|
||||||
self.exclude_paths: list[Path] = []
|
self.exclude_paths: list[Path] = []
|
||||||
self.metadata = VaultMetadata()
|
self.metadata = VaultMetadata()
|
||||||
for p in config.exclude_paths:
|
for p in config.exclude_paths:
|
||||||
@@ -60,7 +58,6 @@ class Vault:
|
|||||||
yield "vault_path", self.vault_path
|
yield "vault_path", self.vault_path
|
||||||
yield "dry_run", self.dry_run
|
yield "dry_run", self.dry_run
|
||||||
yield "backup_path", self.backup_path
|
yield "backup_path", self.backup_path
|
||||||
yield "new_vault", self.new_vault_path
|
|
||||||
yield "num_notes", self.num_notes()
|
yield "num_notes", self.num_notes()
|
||||||
yield "exclude_paths", self.exclude_paths
|
yield "exclude_paths", self.exclude_paths
|
||||||
|
|
||||||
@@ -285,18 +282,10 @@ class Vault:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def write(self, new_vault: bool = False) -> None:
|
def write(self) -> None:
|
||||||
"""Write changes to the vault."""
|
"""Write changes to the vault."""
|
||||||
log.debug("Writing changes to vault...")
|
log.debug("Writing changes to vault...")
|
||||||
if new_vault:
|
if self.dry_run is False:
|
||||||
log.debug("Writing changes to backup")
|
|
||||||
for _note in self.notes:
|
for _note in self.notes:
|
||||||
_new_note_path: Path = Path(
|
log.trace(f"writing to {_note.note_path}")
|
||||||
self.new_vault_path / Path(_note.note_path).relative_to(self.vault_path)
|
|
||||||
)
|
|
||||||
log.debug(f"writing to {_new_note_path}")
|
|
||||||
_note.write(path=_new_note_path)
|
|
||||||
else:
|
|
||||||
for _note in self.notes:
|
|
||||||
log.debug(f"writing to {_note.note_path}")
|
|
||||||
_note.write()
|
_note.write()
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ def test_vault_creation(test_vault):
|
|||||||
|
|
||||||
assert vault.vault_path == vault_path
|
assert vault.vault_path == vault_path
|
||||||
assert vault.backup_path == Path(f"{vault_path}.bak")
|
assert vault.backup_path == Path(f"{vault_path}.bak")
|
||||||
assert vault.new_vault_path == Path(f"{vault_path}.new")
|
|
||||||
assert vault.dry_run is False
|
assert vault.dry_run is False
|
||||||
assert str(vault.exclude_paths[0]) == Regex(r".*\.git")
|
assert str(vault.exclude_paths[0]) == Regex(r".*\.git")
|
||||||
assert vault.num_notes() == 2
|
assert vault.num_notes() == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user