Add embedding guide for gitsync · Entire

Add embedding guide for gitsync

8414f70→main·

Soph·3mo ago·3 files·+164 added/-0 removed

Sessions

be52c896e43eView transcript

Changes

3

82 unmodified lines

83
84
85
86
87
88
89
90

82 unmodified lines

- `Execution`  
  - execution mode, protocol, relay summary, and batch summary

See [docs/embedding.md](docs/embedding.md) for worker-oriented guidance.

## Current scope

- Smart HTTP only

MREADME.md+2

185 unmodified lines

186 187 188 189

185 unmodified lines

Embedding

git-sync can now be used as a library as well as a CLI.

For most embedders, there are two important rules:

Stable vs Unstable

Use pkg/gitsync when you want a durable worker-facing API:

Use pkg/gitsync/unstable only when you need controls that are intentionally not yet stable:

The CLI and benchmark command use pkg/gitsync/unstable because they still need those controls. External workers should generally not.

Worker Shape

A queue worker usually wants:

  1. deserialize a job into source, target, scope, and policy
  2. build a gitsync.Client
  3. inject auth and an http.Client
  4. call Plan or Sync
  5. persist structured result data
  6. decide success, retry, or escalation

Minimal example:

package worker

import (
    "context"
    "net/http"

"github.com/soph/git-sync/pkg/gitsync"
)

func runSync(ctx context.Context) error {
    client := gitsync.New(gitsync.Options{
        HTTPClient: &http.Client{},
        Auth: gitsync.StaticAuthProvider{
            Source: gitsync.EndpointAuth{Token: "source-token"},
            Target: gitsync.EndpointAuth{Token: "target-token"},
        },
    })

result, err := client.Sync(ctx, gitsync.SyncRequest{
        Source: gitsync.Endpoint{URL: "https://github.example/source/repo.git"},
        Target: gitsync.Endpoint{URL: "https://git.example/target/repo.git"},
        Scope: gitsync.RefScope{
            Branches: []string{"main"},
        },
        Policy: gitsync.SyncPolicy{
            IncludeTags: true,
            Protocol: gitsync.ProtocolAuto,
        },
    })
    if err != nil {
        return err
    }

_ = result
    return nil
}

Auth Injection

pkg/gitsync uses one auth ownership model:

That avoids baking CLI-style precedence rules into request types.

Good uses of AuthProvider:

The simplest option is gitsync.StaticAuthProvider, but a real worker will usually implement AuthProvider itself.

HTTP Injection

Pass an *http.Client through gitsync.Options when you need:

git-sync clones and wraps the provided client internally so it can still collect transfer stats without mutating the caller's client directly.

Result Handling

The stable SyncResult is organized for worker consumption:

That gives a worker enough structure to:

Retry Guidance

Treat these differently:

For many workers, a useful pattern is:

What Not To Depend On

If you want stability, do not build external worker logic around:

Those are implementation details or advanced controls that currently belong in pkg/gitsync/unstable, not the stable embedding contract.