gitsync

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gitsync records vault changes in git and keeps the checkout in step with the team: it commits okvault's writes, rebases on the remote and pushes, either straight to a branch (push mode) or to a personal branch with one open GitHub pull request (pr mode). See config.Git for the settings.

It shells out to the git CLI so that the user's credential helpers, SSH keys and hooks apply exactly as they do on the command line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckOptions added in v0.2.0

type CheckOptions struct {
	Remote   string              // default origin
	Env      func(string) string // nil: no environment
	PushTest bool                // also run git push --dry-run
	API      string              // override the API base URL (tests)
	HTTP     *http.Client
}

CheckOptions configure CheckGitHub.

type GitHub

type GitHub struct {
	API   string // e.g. https://api.github.com, or https://HOST/api/v3 for GitHub Enterprise Server
	Web   string // e.g. https://github.com
	Owner string
	Repo  string
	Token string
	// TokenSource says where Token came from (never the value).
	TokenSource string
	HTTP        *http.Client
}

GitHub is a minimal GitHub REST client for the pull requests of one repository.

func (*GitHub) CompareURL

func (g *GitHub) CompareURL(base, head string) string

CompareURL is the page where a person can open the pull request by hand.

func (*GitHub) EnsurePR

func (g *GitHub) EnsurePR(ctx context.Context, head, base, title, body string) (*PullRequest, error)

EnsurePR opens a pull request from head into base, or updates the body of the one already open.

func (*GitHub) LatestMerged

func (g *GitHub) LatestMerged(ctx context.Context, head string) (*PullRequest, error)

LatestMerged returns the most recently merged pull request from head, or nil when one is still open or none was merged.

type GitHubCheck added in v0.2.0

type GitHubCheck struct {
	RemoteName string `json:"remote_name"`
	Remote     string `json:"remote,omitempty"` // URL, credentials removed
	Repo       string `json:"repo,omitempty"`   // owner/name, when the remote is on GitHub
	Token      string `json:"token"`            // API token source: GH_TOKEN, GITHUB_TOKEN, GitHub CLI, or none
	User       string `json:"user,omitempty"`   // the token's GitHub login
	CanPush    *bool  `json:"can_push,omitempty"`
	APIProblem string `json:"api_problem,omitempty"` // pull requests (pr mode) will not open
	Push       string `json:"push,omitempty"`        // git push --dry-run: "ok" or what failed; empty when not checked
	Problem    string `json:"problem,omitempty"`     // no repository or no remote
}

GitHubCheck is what okvault can reach of the vault's GitHub repository with this machine's credentials. It names where a token came from, never its value.

func CheckGitHub added in v0.2.0

func CheckGitHub(ctx context.Context, vaultRoot string, o CheckOptions) GitHubCheck

CheckGitHub reports whether okvault can open pull requests for the vault's repository (an API token that can push) and push to it (git credentials).

type Options

type Options struct {
	Vault  *vault.Vault
	Config func() config.Git // current settings; read on every operation so config edits apply live
	Mode   string            // overrides Config().Mode when set (`serve --git`)
	Author string            // agent name, used for the default branch when git has no user.name
	Env    func(string) string
	// GitHub overrides the client built from the remote URL (tests).
	GitHub *GitHub
}

Options configures a Syncer.

type PullRequest

type PullRequest struct {
	Number   int        `json:"number"`
	URL      string     `json:"html_url"`
	State    string     `json:"state"` // open or closed
	MergedAt *time.Time `json:"merged_at"`
	Head     struct {
		SHA string `json:"sha"`
		Ref string `json:"ref"`
	} `json:"head"`
}

PullRequest is the subset of a GitHub pull request okvault uses.

type Status

type Status struct {
	Mode     string    `json:"mode"`
	Branch   string    `json:"branch,omitempty"`
	Base     string    `json:"base,omitempty"`     // pr mode: target branch
	Unpushed int       `json:"unpushed,omitempty"` // commits not yet on the remote
	PR       string    `json:"pull_request,omitempty"`
	Problem  string    `json:"problem,omitempty"` // why the last sync did not finish
	LastSync time.Time `json:"last_sync,omitzero"`
}

Status is the state of the vault's git sync, as shown to agents and people.

type Syncer

type Syncer struct {
	// contains filtered or unexported fields
}

Syncer commits vault changes and syncs them with the remote.

func New

func New(o Options) (*Syncer, error)

New opens the git repository holding the vault.

func (*Syncer) Begin

func (s *Syncer) Begin() (func(), error)

Begin implements vault.Committer: the vault calls it before writing files, so no other okvault process is rebasing the tree it writes into.

func (*Syncer) Commit

func (s *Syncer) Commit(paths []string, subject, body string) (string, error)

Commit implements vault.Committer: it commits exactly the given vault paths. The vault calls it between Begin and its end function.

func (*Syncer) CommitEdits

func (s *Syncer) CommitEdits(ctx context.Context) (string, error)

CommitEdits commits every uncommitted change in the vault (edits made in Obsidian or by hand), after regenerating indexes so they match.

func (*Syncer) InProgress

func (s *Syncer) InProgress() bool

InProgress reports whether git is in the middle of a rebase, merge or cherry-pick in this checkout, by any process (okvault or a person). Index regeneration must wait until it is over.

func (*Syncer) Kick

func (s *Syncer) Kick()

Kick asks the background loop to sync soon (after a commit).

func (*Syncer) Mode

func (s *Syncer) Mode() string

Mode is the effective git mode.

func (*Syncer) Run

func (s *Syncer) Run(ctx context.Context)

Run commits edits made outside okvault when they have rested (commit_edits) and syncs after okvault's commits and every pull_interval, until ctx ends.

func (*Syncer) Status

func (s *Syncer) Status() Status

Status returns the last known sync state.

func (*Syncer) Sync

func (s *Syncer) Sync(ctx context.Context) Status

Sync fetches, rebases local commits on the remote and pushes them (push mode), or keeps the personal branch rebased on the base branch, pushed, and proposed in one pull request (pr mode). It returns the resulting status.

Only the rebase holds the vault's write lock: fetching, pushing and the GitHub API run without it, so kb_write never waits on the network.

func (*Syncer) Touched

func (s *Syncer) Touched()

Touched records that the watcher saw files change (for commit_edits).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL