Skip to content

[SR-13484] Use --no-checkout when cloning packages, since it's immediately followed by a checkout anyway #2903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/SourceControl/GitRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class GitRepositoryProvider: RepositoryProvider {
// a clone from our cache of repositories and then we replace the remote to the one originally
// present in the bare repository.
try Process.checkNonZeroExit(args:
Git.tool, "clone", sourcePath.pathString, destinationPath.pathString)
Git.tool, "clone", "--no-checkout", sourcePath.pathString, destinationPath.pathString)
// The default name of the remote.
let origin = "origin"
// In destination repo remove the remote which will be pointing to the source repo.
Expand All @@ -102,7 +102,7 @@ public class GitRepositoryProvider: RepositoryProvider {
// only ever expect to get back a revision that remains present in the
// object storage.
try Process.checkNonZeroExit(args:
Git.tool, "clone", "--shared", sourcePath.pathString, destinationPath.pathString)
Git.tool, "clone", "--shared", "--no-checkout", sourcePath.pathString, destinationPath.pathString)
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/SourceControl/InMemoryGitRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ public final class InMemoryGitRepositoryProvider: RepositoryProvider {
) throws {
let checkout = fetchedMap[sourcePath]!.copy(at: destinationPath)
checkoutsMap[destinationPath] = checkout
try checkout.installHead()
}

public func checkoutExists(at path: AbsolutePath) throws -> Bool {
Expand Down
5 changes: 4 additions & 1 deletion Sources/SourceControl/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public protocol RepositoryProvider {

/// Clone a managed repository into a working copy at on the local file system.
///
/// Once complete, the repository can be opened using `openCheckout`.
/// Once complete, the repository can be opened using `openCheckout`. Note
/// that there is no requirement that the files have been materialized into
/// the file system at the completion of this call, since it will always be
/// followed by checking out the cloned working copy at a particular ref.
///
/// - Parameters:
/// - sourcePath: The location of the repository on disk, at which the
Expand Down
39 changes: 39 additions & 0 deletions Tests/SourceControlTests/GitRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,43 @@ class GitRepositoryTests: XCTestCase {
XCTAssertTrue(ignored[0])
}
}

func testMissingDefaultBranch() throws {
mktmpdir { path in
// Create a repository.
let testRepoPath = path.appending(component: "test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)

// Create a `main` branch and remove `master`.
try repo.checkout(newBranch: "main")
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "branch", "-D", "master"])

// Change the branch name to something non-existent.
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "symbolic-ref", "HEAD", "refs/heads/_non_existent_branch_"])

// Clone it somewhere.
let testClonePath = path.appending(component: "clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(url: testRepoPath.pathString)
try provider.fetch(repository: repoSpec, to: testClonePath)
let clonedRepo = provider.open(repository: repoSpec, at: testClonePath)
XCTAssertEqual(clonedRepo.tags, [])

// Clone off a checkout.
let checkoutPath = path.appending(component: "checkout")
try provider.cloneCheckout(repository: repoSpec, at: testClonePath, to: checkoutPath, editable: false)
XCTAssertFalse(localFileSystem.exists(checkoutPath.appending(component: "file.swift")))
let checkoutRepo = try provider.openCheckout(at: checkoutPath)

// Try to check out the `main` branch.
try checkoutRepo.checkout(revision: Revision(identifier: "main"))
XCTAssertTrue(localFileSystem.exists(checkoutPath.appending(component: "file.swift")))

// The following will throw if HEAD was set incorrectly and we didn't do a no-checkout clone.
XCTAssertNoThrow(try checkoutRepo.getCurrentRevision())
}
}

}