Skip to content

Commit 48101cb

Browse files
committed
[Commands] Don't blow away modified Packages.
- This adds a limited safety check that we don't let the current update workflow remove any modified repositories under `Packages`. It doesn't detect modifications to untracked files, but it should catch most cases. - Will add test coverage for this in the swift-integration-tests repo.
1 parent 1542ff6 commit 48101cb

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
@@ -169,6 +169,22 @@ public struct SwiftPackageTool {
169169
try initPackage.writePackageStructure()
170170

171171
case .update:
172+
// Attempt to ensure that none of the repositories are modified.
173+
for item in walk(opts.path.Packages, recursively: false) {
174+
// Only look at repositories.
175+
guard Path.join(item, ".git").exists else { continue }
176+
177+
// If there is a staged or unstaged diff, don't remove the
178+
// tree. This won't detect new untracked files, but it is
179+
// just a safety measure for now.
180+
let diffArgs = ["--no-ext-diff", "--quiet", "--exit-code"]
181+
do {
182+
_ = try Git.runPopen([Git.tool, "-C", item, "diff"] + diffArgs)
183+
_ = try Git.runPopen([Git.tool, "-C", item, "diff", "--cached"] + diffArgs)
184+
} catch {
185+
throw Error.repositoryHasChanges(item)
186+
}
187+
}
172188
try Utility.removeFileTree(opts.path.Packages)
173189
fallthrough
174190

0 commit comments

Comments
 (0)