Skip to content

Commit 40e6d79

Browse files
noefroidevauxaciidgh
authored andcommitted
[SR-5437] Check for untracked files (swiftlang#1396)
* SR-5437 Check for untracked files * SR-5437 Add comment * SR-5437 Use Process.checkNonZeroExit
1 parent fd8af9d commit 40e6d79

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Sources/SourceControl/GitRepository.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,11 @@ public class GitRepository: Repository, WorkingCheckout {
302302
// Only a work tree can have changes.
303303
guard isWorkingRepo else { return false }
304304
return queue.sync {
305-
// Detect if there is a staged or unstaged diff.
306-
// This won't detect new untracked files, but it is
307-
// just a safety measure for now.
308-
let args = [Git.tool, "-C", path.asString, "diff", "--no-ext-diff", "--quiet", "--exit-code"]
309-
var nonZeroExit = false
310-
311-
for args in [args, args + ["--cached"]] {
312-
let result = try? Process.popen(arguments: args)
313-
nonZeroExit = nonZeroExit || result?.exitStatus != .terminated(code: 0)
305+
// Check nothing has been changed
306+
guard let result = try? Process.checkNonZeroExit(args: Git.tool, "-C", path.asString, "status", "-s") else {
307+
return false
314308
}
315-
316-
return nonZeroExit
309+
return !result.chomp().isEmpty
317310
}
318311
}
319312

Tests/SourceControlTests/GitRepositoryTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,13 @@ class GitRepositoryTests: XCTestCase {
402402
// Create a file (which we will modify later).
403403
try localFileSystem.writeFileContents(testRepoPath.appending(component: "test.txt"), bytes: "Hi")
404404
let repo = GitRepository(path: testRepoPath)
405+
406+
XCTAssert(repo.hasUncommitedChanges())
407+
405408
try repo.stage(file: "test.txt")
409+
410+
XCTAssert(repo.hasUncommitedChanges())
411+
406412
try repo.commit()
407413

408414
XCTAssertFalse(repo.hasUncommitedChanges())

0 commit comments

Comments
 (0)