Matt Brockman 77b08f53e1
Feature: Add Git Support (#1101)
# Sandbox Git Commands

Adds Git support to the sandbox class. This allows the sandbox to manage
git via standard clone, checkout, branch, add, pull, and push commands
without needing to use commands.run. The API mirrors common Git
workflows while handling sandbox-specific concerns like auth injection
and safe remote handling.

**Python example**
```python
from e2b import Sandbox

sandbox = Sandbox.create()
repo_path = '/home/user/my-repo'

# Optional: set author for commits
sandbox.git.configure_user('Your Name', 'you@example.com')

# Clone or init
sandbox.git.clone('https://github.com/org/repo.git', path=repo_path)
# or
sandbox.git.init(repo_path, initial_branch='main')

# Make a change
sandbox.files.write(f'{repo_path}/README.md', '# Hello\n')

# Commit
sandbox.git.add(repo_path, files=['README.md'])
sandbox.git.commit(repo_path, message='Initial commit')

# Branching
sandbox.git.create_branch(repo_path, 'feature1')
# or
sandbox.git.checkout_branch(repo_path, 'main')

# Push
sandbox.git.remote_add(repo_path, 'origin', 'https://github.com/org/repo.git', overwrite=True)
sandbox.git.push(repo_path, remote='origin', branch='main', set_upstream=True)
```

**JavaScript / TypeScript example**
```ts
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()
const repoPath = '/home/user/my-repo'

await sandbox.git.configureUser('Your Name', 'you@example.com')

await sandbox.git.clone('https://github.com/org/repo.git', { path: repoPath })
// or
await sandbox.git.init(repoPath, { initialBranch: 'main' })

await sandbox.files.write(`${repoPath}/README.md`, '# Hello\n')

await sandbox.git.add(repoPath, { files: ['README.md'] })
await sandbox.git.commit(repoPath, { message: 'Initial commit' })

await sandbox.git.createBranch(repoPath, 'feature1')
await sandbox.git.checkoutBranch(repoPath, 'main')

await sandbox.git.remoteAdd(repoPath, 'origin', 'https://github.com/org/repo.git', {
  overwrite: true,
})
await sandbox.git.push(repoPath, { remote: 'origin', branch: 'main', setUpstream: true })
```

**Main commands**
- `clone`: Clone a repo into the sandbox. Supports `branch`, `depth`,
optional `username` + `password` for private repos, and
`dangerously_store_credentials` / `dangerouslyStoreCredentials` to keep
credentials in the remote URL.
- `init`: Initialize a new repo. Supports `initial_branch` /
`initialBranch` and `bare`.
- `status`: Get parsed `git status --porcelain -b` info.
- `branches`: List branches and current branch.
- `create_branch` / `createBranch`: Create and check out a new branch.
- `checkout_branch` / `checkoutBranch`: Switch to an existing branch.
- `delete_branch` / `deleteBranch`: Delete a branch. Supports `force`.
- `add`: Stage files. Supports explicit files or `all`.
- `commit`: Create a commit. Supports author override and `allow_empty`.
- `reset` / `reset`: Reset `HEAD` (supports modes like `soft`, `mixed`,
`hard`, etc.) and optional paths.
- `restore` / `restore`: Restore files or unstage changes (`worktree` /
`staged`) from a source ref.
- `pull`: Pull from a remote. Supports `remote`, `branch`, and optional
auth.
- `push`: Push to a remote. Supports `remote`, `branch`, `set_upstream`,
and optional auth.
- `remote_add` / `remoteAdd`: Add a remote. Supports `overwrite` and
`fetch`.
- `remote_get` / `remoteGet`: Read a remote URL.
- `set_config` / `setConfig`: Set a git config value. Supports `scope`
(`global`, `local`, `system`), and `path` for local scope.
- `get_config` / `getConfig`: Read a git config value. Supports the same
`scope` options and returns `None` / `undefined` if unset.
- `dangerously_authenticate` / `dangerouslyAuthenticate`: Persist
credentials via the git credential helper (global).
- `configure_user` / `configureUser`: Set default `user.name` and
`user.email` for commits.
- `create_github_repo` (Python only): Create a GitHub repo from inside
the sandbox and optionally add it as a remote.

**Status shape**
- `status` returns a `GitStatus` with `current_branch` /
`currentBranch`, `upstream`, `ahead`, `behind`, `detached`, and
`file_status` / `fileStatus`.
- `file_status` entries include `name`, `status`, `index_status` /
`indexStatus`, `working_tree_status` / `workingTreeStatus`, `staged`,
and optional `renamed_from` / `renamedFrom`.
- Convenience helpers include: `is_clean` / `isClean`, `has_changes` /
`hasChanges`, `has_staged` / `hasStaged`, `has_untracked` /
`hasUntracked`, `has_conflicts` / `hasConflicts`, plus counts
(`total_count` / `totalCount`, `staged_count` / `stagedCount`,
`unstaged_count` / `unstagedCount`, `untracked_count` /
`untrackedCount`, `conflict_count` / `conflictCount`). In Python these
are properties on the `GitStatus` object; in JS they are fields on the
returned object.

**Notes**
- For private HTTPS remotes, pass `username` + `password` (token) on
`clone`, `pull`, or `push`.
- Use `remote_add` / `remoteAdd` with `overwrite=True` to update an
existing remote URL and `fetch=True` to fetch after.
- Use `dangerously_authenticate` / `dangerouslyAuthenticate` only when
you want to persist credentials globally on the sandbox.
2026-01-29 11:13:15 -08:00

89 lines
2.9 KiB
Python

import pytest
@pytest.mark.skip_debug()
def test_status_reports_untracked_file(git_sandbox, git_repo):
git_sandbox.files.write(f"{git_repo}/README.md", "hello\n")
status = git_sandbox.git.status(git_repo)
entry = next(
(item for item in status.file_status if item.name == "README.md"), None
)
assert entry is not None
assert entry.status == "untracked"
assert status.is_clean is False
assert status.has_changes is True
assert status.has_untracked is True
assert status.has_staged is False
assert status.has_conflicts is False
assert status.total_count == 1
assert status.staged_count == 0
assert status.unstaged_count == 1
assert status.untracked_count == 1
assert status.conflict_count == 0
@pytest.mark.skip_debug()
def test_status_reports_added_modified_deleted_renamed(
git_sandbox, git_repo, git_author
):
author_name, author_email = git_author
git_sandbox.files.write(f"{git_repo}/README.md", "hello\n")
git_sandbox.files.write(f"{git_repo}/DELETE.md", "delete me\n")
git_sandbox.files.write(f"{git_repo}/RENAME.md", "rename me\n")
git_sandbox.git.add(git_repo, all=True)
git_sandbox.git.commit(
git_repo,
message="Initial commit",
author_name=author_name,
author_email=author_email,
)
git_sandbox.files.write(f"{git_repo}/README.md", "hello again\n")
git_sandbox.files.write(f"{git_repo}/NEW.md", "new file\n")
git_sandbox.git.add(git_repo, files=["NEW.md"])
git_sandbox.commands.run(f'git -C "{git_repo}" rm DELETE.md')
git_sandbox.commands.run(f'git -C "{git_repo}" mv RENAME.md RENAMED.md')
status = git_sandbox.git.status(git_repo)
modified = next(
(item for item in status.file_status if item.name == "README.md"), None
)
added = next((item for item in status.file_status if item.name == "NEW.md"), None)
deleted = next(
(item for item in status.file_status if item.name == "DELETE.md"), None
)
renamed = next(
(item for item in status.file_status if item.name == "RENAMED.md"),
None,
)
assert modified is not None
assert modified.status == "modified"
assert modified.staged is False
assert added is not None
assert added.status == "added"
assert added.staged is True
assert deleted is not None
assert deleted.status == "deleted"
assert deleted.staged is True
assert renamed is not None
assert renamed.status == "renamed"
assert renamed.staged is True
assert renamed.renamed_from == "RENAME.md"
assert status.has_changes is True
assert status.has_staged is True
assert status.has_untracked is False
assert status.has_conflicts is False
assert status.total_count == 4
assert status.staged_count == 3
assert status.unstaged_count == 1
assert status.untracked_count == 0
assert status.conflict_count == 0