Skip to content

Commit ad06f60

Browse files
author
Miguel Perez
committed
Validate we have a git repo before updating
1 parent 01967cc commit ad06f60

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,14 @@ extension SwiftPackageTool {
311311
guard let configPath = try swiftTool.getConfigPath() else {
312312
throw InternalError("Could not find config path")
313313
}
314-
315-
guard let cwd = localFileSystem.currentWorkingDirectory else {
316-
throw InternalError("Could not get the current working directory")
317-
}
318-
314+
319315
if let path = try? AbsolutePath(validating: url) {
320316
if localFileSystem.exists(path) {
321317
let currentTemplateLocation = path
322318
let templateDirectory = configPath.appending(components: "templates", "new-package", url)
323319

324320
guard localFileSystem.exists(currentTemplateLocation) && localFileSystem.isDirectory(currentTemplateLocation) else {
325-
throw InternalError("Template folder \(url) could not be found in: \(cwd)")
321+
throw StringError("Could not find template: \(url)")
326322
}
327323

328324
try localFileSystem.copy(from: currentTemplateLocation, to: templateDirectory)
@@ -360,13 +356,17 @@ extension SwiftPackageTool {
360356
}
361357

362358
guard localFileSystem.exists(configPath.appending(components: "templates", "new-package", templateName)) else {
363-
throw InternalError("Could not find \(templateName)")
359+
throw StringError("Could not find template: \(templateName)")
364360
}
365361

366362
let provider = GitRepositoryProvider()
367363
let templatePath = configPath.appending(components: "templates", "new-package", templateName)
368364

369-
try provider.pull(repository: RepositorySpecifier(url: templatePath.pathString), to: templatePath)
365+
if provider.isValidDirectory(templatePath.pathString) {
366+
try provider.pull(templatePath.pathString)
367+
} else {
368+
throw StringError("Template: \(templateName) is not a git repo, and therefore could not be updated")
369+
}
370370
}
371371
}
372372

Sources/SourceControl/GitRepository.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public struct GitRepositoryProvider: RepositoryProvider {
107107
try self.fetch(repository: repository, to: path, mirror: true)
108108
}
109109

110-
public func pull(repository: RepositorySpecifier, to path: AbsolutePath) throws {
111-
try self.callGit("pull", "-C", path.pathString, repository: repository)
110+
public func pull(_ directory: String) throws {
111+
try self.callGit("pull", "-C", directory, repository: RepositorySpecifier(url: directory))
112112
}
113113

114114
public func isValidDirectory(_ directory: String) -> Bool {

0 commit comments

Comments
 (0)