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 ¶
- type CheckOptions
- type GitHub
- type GitHubCheck
- type Options
- type PullRequest
- type Status
- type Syncer
- func (s *Syncer) Begin() (func(), error)
- func (s *Syncer) Commit(paths []string, subject, body string) (string, error)
- func (s *Syncer) CommitEdits(ctx context.Context) (string, error)
- func (s *Syncer) InProgress() bool
- func (s *Syncer) Kick()
- func (s *Syncer) Mode() string
- func (s *Syncer) Run(ctx context.Context)
- func (s *Syncer) Status() Status
- func (s *Syncer) Sync(ctx context.Context) Status
- func (s *Syncer) Touched()
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 ¶
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 ¶
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 (*Syncer) Begin ¶
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 ¶
Commit implements vault.Committer: it commits exactly the given vault paths. The vault calls it between Begin and its end function.
func (*Syncer) CommitEdits ¶
CommitEdits commits every uncommitted change in the vault (edits made in Obsidian or by hand), after regenerating indexes so they match.
func (*Syncer) InProgress ¶
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) Run ¶
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) Sync ¶
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.