Git
sandbox.git runs git in the sandbox with typed results: status, log, diff, branches and remotes come back as data, not as
text to parse.
from svm import Sandbox
with Sandbox.create(template="base") as sandbox: git = sandbox.git repo = git.clone("https://github.com/octocat/Hello-World.git", "/tmp/hello", depth=1) git.create_branch(repo, "docs-change")
sandbox.files.write(f"{repo}/NOTES.md", "Edited in an SVM sandbox.\n") git.add(repo) commit_id = git.commit(repo, "Add notes") print("committed", commit_id)
status = git.status(repo) print(status.branch, "clean" if status.clean else status.files) for commit in git.log(repo, limit=3): print(commit.id[:8], commit.subject, "-", commit.author_name)import { Sandbox } from "@svm/sandbox";
const sandbox = await Sandbox.create({ template: "base" });try { const repo = await sandbox.git.clone("https://github.com/octocat/Hello-World.git", "/home/user/hello", { depth: 1 }); await sandbox.git.createBranch(repo, "docs-change");
await sandbox.files.write(`${repo}/NOTES.md`, "Edited in an SVM sandbox.\n"); await sandbox.git.add(repo); const sha = await sandbox.git.commit(repo, "Add notes"); console.log("committed", sha);
const status = await sandbox.git.status(repo); console.log(status.branch, status.clean, status.files); for (const c of await sandbox.git.log(repo, { limit: 3 })) { console.log(c.id.slice(0, 7), c.authorName, c.subject); }} finally { await sandbox.kill();}git := sandbox.Git()repo := "/tmp/hello-world"if err := git.Clone(ctx, "https://github.com/octocat/Hello-World.git", repo, svm.GitCloneOptions{Depth: 1}); err != nil { return err} return err}if err := git.CreateBranch(ctx, repo, "docs-change", "", true); err != nil { return err}if _, err := files.Write(ctx, repo+"/NOTES.md", []byte("Edited in a sandbox.\n"), 0o644); err != nil { return err}if err := git.Add(ctx, repo, "NOTES.md"); err != nil { return err}commit, err := git.Commit(ctx, repo, "Add notes", svm.GitCommitOptions{})if err != nil { return err}fmt.Println("committed", commit)
status, err := git.Status(ctx, repo)if err != nil { return err}fmt.Println("on", status.Branch, "changed files:", len(status.Files))history, err := git.Log(ctx, repo, 5, "")if err != nil { return err}for _, c := range history { fmt.Println(c.ID[:7], c.Subject)}require "svm"
box = SVM::Sandbox.create(template: "base")begin git = box.git repo = "/home/user/hello" git.clone("https://github.com/octocat/Hello-World.git", repo, depth: 1) git.create_branch(repo, "docs-change")
box.files.write("#{repo}/NOTES.md", "Changed from the SVM sandbox.\n") git.add(repo) commit_id = git.commit(repo, "Add notes") puts "committed #{commit_id}"
puts git.status(repo).inspect git.log(repo, limit: 3).each { |entry| puts entry.inspect }ensure box.killendimport dev.svm.sandbox.Client;import dev.svm.sandbox.Git;import dev.svm.sandbox.Sandbox;
public class GitExample { public static void main(String[] args) { Client client = Client.fromEnvironment(); try (Sandbox sandbox = Sandbox.create(client, "base")) { Git git = sandbox.git(); String repo = "/home/user/hello";
git.clone("https://github.com/octocat/Hello-World.git", repo, null, 1, null); git.createBranch(repo, "docs-change");
sandbox.files().write(repo + "/NOTES.md", "written from the sandbox\n"); git.add(repo); String commit = git.commit(repo, "Add notes", null, null, false); System.out.println("committed " + commit);
Git.Status status = git.status(repo); System.out.println("on " + status.branch + ", clean: " + status.clean()); for (Git.Commit c : git.log(repo, 3, null)) { System.out.println(c.id.substring(0, 7) + " " + c.subject); } } }}Private repositories
Section titled “Private repositories”For a private repository, use one of these. The token never enters the sandbox:
- Create the sandbox with git credentials, then call
install_credentials. A credential helper then answers through the platform. - Or bind a token to the host with an egress inject rule (Secrets).
egress_envgives the environment that tells git to use it.