Skip to content

Commit 12e23aa

Browse files
committed
讓自動提交包含同步狀態檔
1 parent 02ef7b7 commit 12e23aa

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

app/git_ops.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from git.exc import GitCommandError, InvalidGitRepositoryError, NoSuchPathError
88

99
from .models import SyncTask
10+
from .sync_state import MANIFEST_NAME
1011

1112

1213
class GitManager:
@@ -33,10 +34,12 @@ def commit_task_changes(self, task: SyncTask) -> str | None:
3334
raise ValueError("output directory must be inside the Git working tree")
3435
relative_target = target_dir.relative_to(working_tree)
3536
scope = "." if str(relative_target) == "." else str(relative_target)
36-
scoped_status = repo.git.status("--porcelain", "--", scope)
37+
manifest_path = working_tree / MANIFEST_NAME
38+
manifest_scope = manifest_path.relative_to(working_tree).as_posix()
39+
scoped_status = repo.git.status("--porcelain", "--", scope, manifest_scope)
3740
if not scoped_status.strip():
3841
return None
39-
repo.git.add("--all", "--", scope)
42+
repo.git.add("--all", "--", scope, manifest_scope)
4043
message = task.git.commit_message_template.format(task_name=task.name)
4144
commit = repo.index.commit(message)
4245
if task.git.auto_push:

app/sync_engine.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
from .git_ops import GitManager
1919
from .models import SyncEvent, SyncTask, TaskStatus
2020
from .storage import Storage
21-
22-
MANIFEST_NAME = ".office-docs-sync-state.json"
23-
MANIFEST_VERSION = 1
21+
from .sync_state import MANIFEST_NAME, MANIFEST_VERSION
2422

2523

2624
class TaskEventHandler(FileSystemEventHandler):

app/sync_state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from __future__ import annotations
2+
3+
MANIFEST_NAME = ".office-docs-sync-state.json"
4+
MANIFEST_VERSION = 1

tests/test_sync_engine.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ def test_sync_engine_commits_inside_target_root(tmp_path: Path) -> None:
5252
assert engine.statuses[task.id or 0].last_error is None
5353
assert (Path(task.paths.target_root) / ".git").exists()
5454
assert (Path(task.paths.target_root) / task.paths.output_subdir / "123.md.md").exists()
55+
assert (Path(task.paths.target_root) / MANIFEST_NAME).exists()
56+
57+
58+
def test_sync_engine_git_commit_includes_manifest(tmp_path: Path) -> None:
59+
storage = Storage(tmp_path / "app.db")
60+
task = create_task(storage, tmp_path, git_enabled=True)
61+
engine = SyncEngine(storage, Converter(), GitManager())
62+
source_file = Path(task.paths.source_dir) / "123.md"
63+
source_file.write_text("hello\n", encoding="utf-8")
64+
65+
engine.queue_path(task.id or 0, str(source_file))
66+
engine.queued_paths[task.id or 0][source_file] = 0
67+
engine._process_task_queue(task)
68+
69+
repo = engine.git_manager.ensure_repo(task)
70+
committed_files = repo.git.show("--name-only", "--pretty=format:", "HEAD").splitlines()
71+
72+
assert task.paths.output_subdir + "/123.md.md" in committed_files
73+
assert MANIFEST_NAME in committed_files
5574

5675

5776
def test_reload_tasks_clears_queue_on_disable_and_removes_deleted_task(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)