Git Integration¶
The git module provides:
- Auto-commit + deferred push for write operations (via
on_write) - Periodic pull (ff-only) primitives used by the server to keep the working tree up to date
Quick Start¶
from pathlib import Path
from markdown_vault_mcp import Collection, GitWriteStrategy
strategy = GitWriteStrategy(
token="ghp_your_token",
push_delay_s=30,
)
collection = Collection(
source_dir=Path("/path/to/vault"),
read_only=False,
on_write=strategy,
)
# Writes are now auto-committed and pushed
collection.write("notes/new.md", "Hello world")
# Clean up on shutdown
collection.close()
API Reference¶
GitWriteStrategy(token=None, username='x-access-token', repo_url=None, managed=False, enable_pull=True, enable_push=True, push_delay_s=30.0, commit_name=None, commit_email=None, git_lfs=True, repo_path=None)
¶
Stateful git strategy: commit per write, deferred push.
On each callback invocation:
- Stages the changed file (
git addorgit add -ufor deletes). - Commits with an auto-generated message (
"operation: path"). - Resets the push timer — push fires after
push_delay_sof idle.
Push is deferred to a background threading.Timer that resets on
each write. When the timer fires (no writes for push_delay_s),
all accumulated local commits are pushed in a single git push.
On startup, any unpushed local commits (from a previous crash) are pushed immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str | None
|
PAT for HTTPS push via |
None
|
username
|
str
|
Username used with token auth. Defaults to
|
'x-access-token'
|
repo_url
|
str | None
|
Remote URL expected in managed mode. |
None
|
managed
|
bool
|
When |
False
|
enable_pull
|
bool
|
Enable fetch + ff-only sync methods. |
True
|
enable_push
|
bool
|
Enable deferred push behavior. |
True
|
push_delay_s
|
float
|
Seconds of idle before pushing. |
30.0
|
commit_name
|
str | None
|
Git committer name; defaults to
:attr: |
None
|
commit_email
|
str | None
|
Git committer email; defaults to
:attr: |
None
|
git_lfs
|
bool
|
When |
True
|
repo_path
|
Path | None
|
Optional repository path used for startup validation.
When set together with |
None
|
Example::
strategy = GitWriteStrategy(token="ghp_...", push_delay_s=30)
collection = Collection(on_write=strategy, ...)
# ... writes happen, push deferred ...
strategy.close() # final flush
Source code in src/markdown_vault_mcp/git.py
__call__(path, content, operation)
¶
WriteCallback interface: stage + commit, then schedule push.
Source code in src/markdown_vault_mcp/git.py
sync_once(repo_path)
¶
Fetch and update once, returning True if HEAD advanced.
Tries fast-forward first; falls back to rebase when the local and upstream branches have diverged (e.g. Obsidian and MCP both committed on different files). Aborts on true conflicts.
Source code in src/markdown_vault_mcp/git.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 | |
start(*, repo_path, pull_interval_s, pause_writes=None, on_pull=None)
¶
Start a periodic fetch + ff-only update loop in a daemon thread.
Source code in src/markdown_vault_mcp/git.py
stop()
¶
Stop the pull loop thread if it is running.
Source code in src/markdown_vault_mcp/git.py
flush()
¶
Block until any pending push completes.
Cancels the idle timer and pushes immediately if there are pending local commits.
Source code in src/markdown_vault_mcp/git.py
git_write_strategy(token=None, push_delay_s=0, git_lfs=True)
¶
Create a :class:GitWriteStrategy callback.
Convenience wrapper around :class:GitWriteStrategy. With the
default push_delay_s=0, commits happen per-write but push only
fires when :meth:~GitWriteStrategy.close or
:meth:~GitWriteStrategy.flush is called.
When used via :class:~markdown_vault_mcp.collection.Collection,
Collection.close() automatically calls the strategy's
close(), so pushes flush on shutdown. Callers using this
as a bare WriteCallback must retain a reference and call
close() explicitly.
.. deprecated::
Prefer :class:GitWriteStrategy directly for access to
:meth:~GitWriteStrategy.flush and :meth:~GitWriteStrategy.close.
.. note::
The default push_delay_s=0 here differs from
:class:GitWriteStrategy's default of 30.0. This preserves
backward compatibility (push on close/flush only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str | None
|
PAT for HTTPS push. |
None
|
push_delay_s
|
float
|
Push delay in seconds (default 0 = push on close only). |
0
|
git_lfs
|
bool
|
When |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
GitWriteStrategy
|
class: |
GitWriteStrategy
|
data: |