native macOS codings agent orchestrator prowl.onev.cat
Something went wrong. Try again.
2.6 kB · 69 lines
Swift
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import Foundationimport Testing
@testable import supacode
struct GitAutomaticBaseRefIntegrationTests { @Test func fallsBackToLocalHeadForBareRepo() async throws { let tempRoot = URL(filePath: "/tmp", directoryHint: .isDirectory) let id = UUID().uuidString let bareURL = tempRoot.appending( path: "supacode-bare-\(id).git", directoryHint: URL.DirectoryHint.isDirectory ) let workURL = tempRoot.appending( path: "supacode-work-\(id)", directoryHint: URL.DirectoryHint.isDirectory ) defer { try? FileManager.default.removeItem(at: bareURL) try? FileManager.default.removeItem(at: workURL) }
try runGit(["init", workURL.path(percentEncoded: false)]) try runGit(["-C", workURL.path(percentEncoded: false), "config", "user.email", "test@example.com"]) try runGit(["-C", workURL.path(percentEncoded: false), "config", "user.name", "Test User"])
let readmeURL = workURL.appending(path: "README.md") try "hello".write(to: readmeURL, atomically: true, encoding: .utf8) try runGit(["-C", workURL.path(percentEncoded: false), "add", "README.md"]) try runGit(["-C", workURL.path(percentEncoded: false), "commit", "-m", "init"]) try runGit(["-C", workURL.path(percentEncoded: false), "branch", "-M", "main"])
try runGit(["init", "--bare", bareURL.path(percentEncoded: false)]) try runGit([ "-C", workURL.path(percentEncoded: false), "remote", "add", "origin", bareURL.path(percentEncoded: false), ]) try runGit(["-C", workURL.path(percentEncoded: false), "push", "-u", "origin", "main"]) try runGit(["--git-dir", bareURL.path(percentEncoded: false), "symbolic-ref", "HEAD", "refs/heads/main"])
let baseRef = await GitClient().automaticWorktreeBaseRef(for: bareURL) #expect(baseRef == "main") }}
private struct GitCommandError: Error { let output: String}
@discardableResultprivate func runGit(_ arguments: [String]) throws -> String { let process = Process() process.executableURL = URL(fileURLWithPath: "/usr/bin/git") process.arguments = arguments var environment = ProcessInfo.processInfo.environment environment["GIT_CONFIG_GLOBAL"] = "/dev/null" process.environment = environment let pipe = Pipe() process.standardOutput = pipe process.standardError = pipe try process.run() process.waitUntilExit() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8) ?? "" if process.terminationStatus != 0 { throw GitCommandError(output: output) } return output}