demogit

package
v0.160.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 34 Imported by: 1 module, 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 demogit keeps the workspace of an SSH demo session on GitHub: it finds or creates the visitor's fork of the canonical demo repository, saves the workspace to a dedicated branch of that fork without ever forcing a push, and clones a saved branch back into a fresh workspace.

Design: spec/features/ssh-demo/README.md (D6, D7, D8). Everything here runs in the per-session child process; the GitHub token is held by the caller in memory and handed to each call, never written anywhere. Git is go-git only: no git binary, no hooks, no filters, no submodules.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNothingToSave: the workspace has no change against its base commit.
	ErrNothingToSave = errors.New("nothing to save: the workspace has no changes")
	// ErrDiverged: the branch on the fork moved since the workspace's base
	// commit; nothing was pushed. Offer NewBranchName.
	ErrDiverged = errors.New("the branch on your fork has changes this workspace does not have")
	// ErrAuth: GitHub refused the token (401, or 403 without rate limiting).
	ErrAuth = errors.New("GitHub refused the sign-in")
	// ErrRateLimited: GitHub rate limited the request; see RateLimitError.
	ErrRateLimited = errors.New("GitHub rate limit reached")
	// ErrForkNotReady: the fork did not become available within ForkWait.
	ErrForkNotReady = errors.New("GitHub has not finished creating the fork")
	// ErrNoFork: the visitor has no fork of the canonical repository.
	ErrNoFork = errors.New("no fork of the demo repository found")
	// ErrForkSearchIncomplete indicates more GitHub repository pages remain to be searched.
	ErrForkSearchIncomplete = errors.New("fork search stopped after 50 pages")
	// ErrGitRedirect indicates a Git HTTP redirect outside the validated repository path or origin.
	ErrGitRedirect = errors.New("git redirect changes repository path or origin")
	// ErrUntrustedContent: the demo could not safely open the workspace.
	ErrUntrustedContent = errors.New("the repository cannot be opened in the demo")
)

Errors callers branch on. Returned errors wrap these; test with errors.Is.

Functions

func InstallClone

func InstallClone(ctx context.Context, checkout, destDir string) error

InstallClone publishes a validated checkout by moving it into an absent or empty destination directory on the same filesystem.

func ValidateWorkspace

func ValidateWorkspace(root, projectDir string, limits CloneLimits) error

ValidateWorkspace rejects a checked-out tree that DataTug must not open: a symlink that resolves outside root, more than limits allow, or no <projectDir>/datatug-project.json. Errors wrap ErrUntrustedContent.

Types

type Change

type Change struct {
	Path string
	// Kind is "added", "modified" or "deleted".
	Kind string
}

Change is one changed path of the workspace, relative to the repository root.

func Changes

func Changes(repoDir string) ([]Change, error)

Changes lists the workspace changes Save would commit, for the save screen. It needs no token.

type Client

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

Client talks to GitHub with one visitor's token.

func New

func New(cfg Config, token string) *Client

New returns a client that authenticates with token. token is never logged, persisted, or put in a URL or git config.

func (*Client) EnsureFork

func (c *Client) EnsureFork(ctx context.Context) (fork Repo, created bool, err error)

EnsureFork returns the visitor's fork, creating it when there is none and waiting until GitHub serves it. created reports whether it was made now. Callers ask the visitor before calling it.

func (*Client) FindFork

func (c *Client) FindFork(ctx context.Context) (Repo, error)

FindFork returns the visitor's fork of the canonical repository, ErrNoFork after a complete search, or ErrForkSearchIncomplete if the search hits its cap.

func (*Client) NewBranchName

func (c *Client) NewBranchName() string

NewBranchName returns the branch offered after ErrDiverged: "<Config.Branch>-<UTC yyyymmdd-hhmmss>".

func (*Client) Open

func (c *Client) Open(ctx context.Context, fork Repo, destDir, projectDir string) (branch string, err error)

Open clones the saved branch of fork (Config.Branch, else the fork's default branch) into an absent or empty destDir. It validates the tree before checkout and the workspace before publishing. Errors wrap ErrUntrustedContent.

func (*Client) Save

func (c *Client) Save(ctx context.Context, repoDir string, fork Repo, opts SaveOptions) (SaveResult, error)

Save commits every change of the workspace at repoDir (a go-git clone whose HEAD is the base commit) and pushes it to the branch of fork, never forcing: a branch that moved since the base is ErrDiverged.

func (*Client) User

func (c *Client) User(ctx context.Context) (string, error)

User returns the login of the token's owner.

type CloneLimits

type CloneLimits struct {
	// MaxBytes is the total checkout byte budget, including Git metadata (default 200 MiB).
	MaxBytes int64
	// MaxFiles is the largest number of checked-out files (default 20 000).
	MaxFiles int
	// Depth is the clone depth (default 1).
	Depth int
}

CloneLimits bound what Open accepts from an untrusted fork.

type Config

type Config struct {
	// Canonical is the demo repository visitors fork (required).
	Canonical Repo
	// Branch is the branch saves go to (default "datatug-demo").
	Branch string
	// APIBaseURL is the GitHub REST API root (default "https://api.github.com/").
	APIBaseURL string
	// GitBaseURL is the root of clone/push URLs (default "https://github.com/");
	// tests point it at a local server or a file:// directory.
	GitBaseURL string
	// HTTPClient is used for the API and for git over HTTP (default: a client
	// whose transport honours HTTPS_PROXY).
	HTTPClient *http.Client
	// ForkWait bounds how long EnsureFork waits for a new fork (default 60s).
	ForkWait time.Duration
	// Limits bound Open.
	Limits CloneLimits
	// Now is the clock, for branch names (default time.Now).
	Now func() time.Time
}

Config configures a Client. Zero fields take the documented defaults.

type RateLimitError

type RateLimitError struct{ Reset time.Time }

RateLimitError carries when GitHub accepts requests again.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

func (*RateLimitError) Unwrap

func (e *RateLimitError) Unwrap() error

type Repo

type Repo struct {
	Owner string
	Name  string
}

Repo names a GitHub repository.

func (Repo) FullName

func (r Repo) FullName() string

FullName returns "owner/name".

type SaveOptions

type SaveOptions struct {
	// Branch overrides Config.Branch, e.g. for the new branch offered after
	// ErrDiverged.
	Branch string
	// Message is the commit message (default "Save DataTug demo workspace").
	Message string
}

SaveOptions customise Save.

type SaveResult

type SaveResult struct {
	Fork      Repo
	Branch    string
	CommitSHA string
	// CommitURL is the commit's page on GitHub.
	CommitURL string
}

SaveResult reports a successful save.

Jump to

Keyboard shortcuts

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