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 ¶
- Variables
- func InstallClone(ctx context.Context, checkout, destDir string) error
- func ValidateWorkspace(root, projectDir string, limits CloneLimits) error
- type Change
- type Client
- func (c *Client) EnsureFork(ctx context.Context) (fork Repo, created bool, err error)
- func (c *Client) FindFork(ctx context.Context) (Repo, error)
- func (c *Client) NewBranchName() string
- func (c *Client) Open(ctx context.Context, fork Repo, destDir, projectDir string) (branch string, err error)
- func (c *Client) Save(ctx context.Context, repoDir string, fork Repo, opts SaveOptions) (SaveResult, error)
- func (c *Client) User(ctx context.Context) (string, error)
- type CloneLimits
- type Config
- type RateLimitError
- type Repo
- type SaveOptions
- type SaveResult
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to GitHub with one visitor's token.
func New ¶
New returns a client that authenticates with token. token is never logged, persisted, or put in a URL or git config.
func (*Client) EnsureFork ¶
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 ¶
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 ¶
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.
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 ¶
RateLimitError carries when GitHub accepts requests again.
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
func (*RateLimitError) Unwrap ¶
func (e *RateLimitError) Unwrap() error
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.