Skip to content

Commit 82b3fb0

Browse files
committed
Merge pull request #373 from ddunbar/preview-1-update-safety
[Commands] Don't blow away modified Packages.
2 parents ba87617 + 3bfd2b8 commit 82b3fb0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/Commands/Error.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum Error: ErrorProtocol {
2323
case invalidInstallation(String)
2424
case invalidSwiftExec(String)
2525
case buildYAMLNotFound(String)
26+
case repositoryHasChanges(String)
2627
}
2728

2829
extension Error: CustomStringConvertible {
@@ -38,6 +39,8 @@ extension Error: CustomStringConvertible {
3839
return "invalid SWIFT_EXEC value: \(value)"
3940
case .buildYAMLNotFound(let value):
4041
return "no build YAML found: \(value)"
42+
case .repositoryHasChanges(let value):
43+
return "repository has changes: \(value)"
4144
}
4245
}
4346
}

Sources/Commands/SwiftPackageTool.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ public struct SwiftPackageTool {
179179
try initPackage.writePackageStructure()
180180

181181
case .update:
182+
// Attempt to ensure that none of the repositories are modified.
183+
for item in walk(opts.path.Packages, recursively: false) {
184+
// Only look at repositories.
185+
guard Path.join(item, ".git").exists else { continue }
186+
187+
// If there is a staged or unstaged diff, don't remove the
188+
// tree. This won't detect new untracked files, but it is
189+
// just a safety measure for now.
190+
let diffArgs = ["--no-ext-diff", "--quiet", "--exit-code"]
191+
do {
192+
_ = try Git.runPopen([Git.tool, "-C", item, "diff"] + diffArgs)
193+
_ = try Git.runPopen([Git.tool, "-C", item, "diff", "--cached"] + diffArgs)
194+
} catch {
195+
throw Error.repositoryHasChanges(item)
196+
}
197+
}
182198
try Utility.removeFileTree(opts.path.Packages)
183199
fallthrough
184200

0 commit comments

Comments
 (0)