Skip to content

Commit bdc6f70

Browse files
committed
Modify isGitIgnored to be able to process multiple paths in one invocation
This way there is no need to create the shell process for every path separately which can improve the performance
1 parent 5663694 commit bdc6f70

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

Sources/SourceControl/GitRepository.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,19 @@ public class GitRepository: Repository, WorkingCheckout {
398398
}
399399

400400
/// Returns true if the file at `path` is ignored by `git`
401-
public func isIgnored(_ path: AbsolutePath) throws -> Bool {
401+
public func isGitIgnored(_ paths: [AbsolutePath]) throws -> [Bool] {
402402
return try queue.sync {
403-
let result = try Process.popen(args: Git.tool, "-C", self.path.asString, "check-ignore", path.asString)
403+
let stringPaths = paths.map({ $0.asString })
404+
let args = [Git.tool, "-C", self.path.asString, "check-ignore"] + stringPaths
405+
let result = try Process.popen(arguments: args)
404406

405-
switch result.exitStatus {
406-
case .terminated(code: 0):
407-
return true
408-
case .terminated(code: 1):
409-
return false
410-
default:
407+
guard result.exitStatus == .terminated(code: 0) || result.exitStatus == .terminated(code: 1) else {
411408
throw GitInterfaceError.fatalError
412409
}
410+
411+
let output = try result.utf8Output()
412+
413+
return stringPaths.map(output.contains)
413414
}
414415
}
415416

Sources/SourceControl/InMemoryGitRepository.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ extension InMemoryGitRepository: WorkingCheckout {
273273
return true
274274
}
275275

276-
public func isIgnored(_ path: AbsolutePath) throws -> Bool {
277-
return false
276+
public func isGitIgnored(_ paths: [AbsolutePath]) throws -> [Bool] {
277+
return [false]
278278
}
279279
}
280280

Sources/SourceControl/Repository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public protocol WorkingCheckout {
198198
func isAlternateObjectStoreValid() -> Bool
199199

200200
/// Returns true if the file at `path` is ignored by `git`
201-
func isIgnored(_ path: AbsolutePath) throws -> Bool
201+
func isGitIgnored(_ paths: [AbsolutePath]) throws -> [Bool]
202202
}
203203

204204
/// A single repository revision.

Tests/SourceControlTests/GitRepositoryTests.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class GitRepositoryTests: XCTestCase {
605605
}
606606
}
607607

608-
func testIgnored() throws {
608+
func testGitIgnored() throws {
609609
mktmpdir { path in
610610
// Create a repo.
611611
let testRepoPath = path.appending(component: "test-repo")
@@ -614,10 +614,15 @@ class GitRepositoryTests: XCTestCase {
614614
let repo = GitRepository(path: testRepoPath)
615615

616616
// Add a .gitignore
617-
try localFileSystem.writeFileContents(testRepoPath.appending(component: ".gitignore"), bytes: "ignored_file")
617+
try localFileSystem.writeFileContents(testRepoPath.appending(component: ".gitignore"), bytes: "ignored_file1\nignored_file2")
618618

619-
XCTAssertTrue(try repo.isIgnored(testRepoPath.appending(component: "ignored_file")))
620-
XCTAssertFalse(try repo.isIgnored(testRepoPath.appending(component: "not_ignored_file")))
619+
let ignored = try repo.isGitIgnored([testRepoPath.appending(component: "ignored_file1"), testRepoPath.appending(component: "ignored_file2"), testRepoPath.appending(component: "not_ignored")])
620+
XCTAssertTrue(ignored[0])
621+
XCTAssertTrue(ignored[1])
622+
XCTAssertFalse(ignored[2])
623+
624+
let notIgnored = try repo.isGitIgnored([testRepoPath.appending(component: "not_ignored")])
625+
XCTAssertFalse(notIgnored[0])
621626
}
622627
}
623628

@@ -637,6 +642,6 @@ class GitRepositoryTests: XCTestCase {
637642
("testSubmodules", testSubmodules),
638643
("testUncommitedChanges", testUncommitedChanges),
639644
("testAlternativeObjectStoreValidation", testAlternativeObjectStoreValidation),
640-
("testIgnored", testIgnored),
645+
("testGitIgnored", testGitIgnored),
641646
]
642647
}

0 commit comments

Comments
 (0)