Skip to content

Commit a87bf21

Browse files
committed
improved performance by omitting an unnecessary fetch
1 parent 62861ee commit a87bf21

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

Sources/SourceControl/GitRepository.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,24 @@ public class GitRepositoryProvider: RepositoryProvider {
139139
// FIXME: We need infrastructure in this subsystem for reporting
140140
// status information.
141141

142-
let process: Process
143-
144142
if let cache = setupCacheIfNeeded(for: repository) {
145143
// Clone the repository using the cache as a reference if possible.
146144
// Git objects are not shared (--dissociate) to avoid problems that might occur when the cache is
147145
// deleted or the package is copied somewhere it cannot reach the cache directory.
148-
process = Process(args: Git.tool, "clone", "--reference", cache.path.pathString, "--dissociate",
149-
"--mirror", repository.url, path.pathString, environment: Git.environment)
150-
} else {
151-
process = Process(args: Git.tool, "clone", "--mirror",
152-
repository.url, path.pathString, environment: Git.environment)
153-
}
154-
155-
try processSet?.add(process)
156-
157-
try process.launch()
158-
let result = try process.waitUntilExit()
146+
let process = Process(args: Git.tool, "clone", "--mirror",
147+
cache.path.pathString, path.pathString, environment: Git.environment)
148+
try processSet?.add(process)
149+
try process.checkGitError(repository: repository)
159150

160-
// Throw if cloning failed.
161-
guard result.exitStatus == .terminated(code: 0) else {
162-
throw GitCloneError(
163-
repository: repository.url,
164-
result: result
165-
)
151+
let clone = GitRepository(path: path, isWorkingRepo: false)
152+
// In destination repo remove the remote which will be pointing to the cached source repo.
153+
// Set the original remote to the new clone.
154+
try clone.setURL(remote: "origin", url: repository.url)
155+
} else {
156+
let process = Process(args: Git.tool, "clone", "--mirror",
157+
repository.url, path.pathString, environment: Git.environment)
158+
try processSet?.add(process)
159+
try process.checkGitError(repository: repository)
166160
}
167161
}
168162

@@ -904,4 +898,22 @@ extension Process {
904898
}
905899
return try result.utf8Output()
906900
}
901+
902+
/// Execute a git subprocess and get its (UTF-8) output if it has a non zero exit.
903+
/// - Parameter repository: The repository the process operates on
904+
/// - Throws: `GitCloneErrorGitCloneError`
905+
/// - Returns: The process output (stdout + stderr).The process output (stdout + stderr).
906+
@discardableResult
907+
public func checkGitError(repository: RepositorySpecifier) throws -> String {
908+
try launch()
909+
let result = try waitUntilExit()
910+
// Throw if cloning failed.
911+
guard result.exitStatus == .terminated(code: 0) else {
912+
throw GitCloneError(
913+
repository: repository.url,
914+
result: result
915+
)
916+
}
917+
return try result.utf8Output()
918+
}
907919
}

0 commit comments

Comments
 (0)