Skip to content

Commit da50588

Browse files
committed
Move hasBranch to GitClient
1 parent d7e4fca commit da50588

13 files changed

+41
-39
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ extension Analyze {
398398
throw AppError.analysisError(package.model.id, "Package must have default branch")
399399
}
400400

401-
guard try await Current.git.hasBranch(defaultBranch, cacheDir) else {
401+
guard try await git.hasBranch(defaultBranch, at: cacheDir) else {
402402
throw AppError.analysisError(package.model.id, "Default branch '\(defaultBranch)' does not exist in checkout")
403403
}
404404

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ extension AppEnvironment {
4444

4545

4646
struct Git: Sendable {
47-
var hasBranch: @Sendable (Reference, String) async throws -> Bool
4847
var revisionInfo: @Sendable (Reference, String) async throws -> RevisionInfo
4948
var shortlog: @Sendable (String) async throws -> String
5049

5150
static let live: Self = .init(
52-
hasBranch: { ref, path in try await hasBranch(ref, at: path) },
5351
revisionInfo: { ref, path in try await revisionInfo(ref, at: path) },
5452
shortlog: { path in try await shortlog(at: path) }
5553
)

Sources/App/Core/Dependencies/GitClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct GitClient {
2424
var commitCount: @Sendable (_ at: String) async throws -> Int
2525
var firstCommitDate: @Sendable (_ at: String) async throws -> Date
2626
var getTags: @Sendable (_ at: String) async throws -> [Reference]
27+
var hasBranch: @Sendable (Reference, _ at: String) async throws -> Bool
2728
var lastCommitDate: @Sendable (_ at: String) async throws -> Date
2829
}
2930

@@ -34,6 +35,7 @@ extension GitClient: DependencyKey {
3435
commitCount: { path in try await Git.commitCount(at: path) },
3536
firstCommitDate: { path in try await Git.firstCommitDate(at: path) },
3637
getTags: { path in try await Git.getTags(at: path) },
38+
hasBranch: { ref, path in try await Git.hasBranch(ref, at: path) },
3739
lastCommitDate: { path in try await Git.lastCommitDate(at: path) }
3840
)
3941
}

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ final class AnalyzeErrorTests: AppTestCase {
7373
Repository(package: pkgs[1], defaultBranch: "main", name: "2", owner: "foo"),
7474
].save(on: app.db)
7575

76-
Current.git.hasBranch = { @Sendable _, _ in true }
7776
Current.git.revisionInfo = { @Sendable ref, checkoutDir in
7877
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
7978
if checkoutDir.hasSuffix("foo-2") { return .init(commit: "commit \(ref)", date: .t1) }
@@ -103,6 +102,7 @@ final class AnalyzeErrorTests: AppTestCase {
103102
if checkoutDir.hasSuffix("foo-2") { return [.tag(1, 2, 3)] }
104103
throw SetupError()
105104
}
105+
$0.git.hasBranch = { @Sendable _, _ in true }
106106
$0.git.lastCommitDate = { @Sendable _ in .t1 }
107107
$0.httpClient.mastodonPost = { @Sendable [socialPosts = self.socialPosts] message in
108108
socialPosts.withValue { $0.append(message) }

Tests/AppTests/AnalyzerTests.swift

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class AnalyzerTests: AppTestCase {
225225
$0.git.commitCount = { @Sendable _ in 12 }
226226
$0.git.firstCommitDate = { @Sendable _ in .t0 }
227227
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(1, 1, 1)] }
228+
$0.git.hasBranch = { @Sendable _, _ in true }
228229
$0.git.lastCommitDate = { @Sendable _ in .t2 }
229230
$0.httpClient.mastodonPost = { @Sendable _ in }
230231
} operation: {
@@ -250,7 +251,6 @@ class AnalyzerTests: AppTestCase {
250251
packageName: "foo-1",
251252
reference: .tag(1, 0, 0)).save(on: app.db)
252253

253-
Current.git.hasBranch = { @Sendable _, _ in true }
254254
Current.git.revisionInfo = { @Sendable ref, _ in
255255
// simulate the following scenario:
256256
// - main branch has moved from commit0 -> commit3 (timestamp t3)
@@ -318,6 +318,7 @@ class AnalyzerTests: AppTestCase {
318318
$0.fileManager.fileExists = { @Sendable _ in true }
319319
$0.git.commitCount = { @Sendable _ in 12 }
320320
$0.git.firstCommitDate = { @Sendable _ in .t0 }
321+
$0.git.hasBranch = { @Sendable _, _ in false } // simulate analysis error via branch mismatch
321322
$0.git.lastCommitDate = { @Sendable _ in .t1 }
322323
} operation: {
323324
// setup
@@ -326,7 +327,6 @@ class AnalyzerTests: AppTestCase {
326327
try await Repository(package: pkg, defaultBranch: "main").save(on: app.db)
327328
}
328329

329-
Current.git.hasBranch = { @Sendable _, _ in false } // simulate analysis error via branch mismatch
330330
Current.git.shortlog = { @Sendable _ in "" }
331331

332332
// Ensure candidate selection is as expected
@@ -364,6 +364,7 @@ class AnalyzerTests: AppTestCase {
364364
$0.git.commitCount = { @Sendable _ in 12 }
365365
$0.git.firstCommitDate = { @Sendable _ in .t0 }
366366
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0)] }
367+
$0.git.hasBranch = { @Sendable _, _ in true }
367368
$0.git.lastCommitDate = { @Sendable _ in .t1 }
368369
} operation: {
369370
// setup
@@ -374,7 +375,6 @@ class AnalyzerTests: AppTestCase {
374375
}
375376
let lastUpdate = Date()
376377

377-
Current.git.hasBranch = { @Sendable _, _ in true }
378378
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
379379
Current.git.shortlog = { @Sendable _ in
380380
"""
@@ -564,9 +564,9 @@ class AnalyzerTests: AppTestCase {
564564
func test_getIncomingVersions() async throws {
565565
try await withDependencies {
566566
$0.git.getTags = { @Sendable _ in [.tag(1, 2, 3)] }
567+
$0.git.hasBranch = { @Sendable _, _ in true }
567568
} operation: {
568569
// setup
569-
Current.git.hasBranch = { @Sendable _, _ in true }
570570
Current.git.revisionInfo = { @Sendable ref, _ in .init(commit: "sha-\(ref)", date: .t0) }
571571
do {
572572
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
@@ -584,23 +584,26 @@ class AnalyzerTests: AppTestCase {
584584
}
585585

586586
func test_getIncomingVersions_default_branch_mismatch() async throws {
587-
// setup
588-
Current.git.hasBranch = { @Sendable _, _ in false} // simulate branch mismatch
589-
do {
590-
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
591-
try await pkg.save(on: app.db)
592-
try await Repository(id: .id1, package: pkg, defaultBranch: "main").save(on: app.db)
593-
}
594-
let pkg = try await Package.fetchCandidate(app.db, id: .id0)
587+
try await withDependencies {
588+
$0.git.hasBranch = { @Sendable _, _ in false} // simulate branch mismatch
589+
} operation: {
590+
// setup
591+
do {
592+
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
593+
try await pkg.save(on: app.db)
594+
try await Repository(id: .id1, package: pkg, defaultBranch: "main").save(on: app.db)
595+
}
596+
let pkg = try await Package.fetchCandidate(app.db, id: .id0)
595597

596-
// MUT
597-
do {
598-
_ = try await Analyze.getIncomingVersions(client: app.client, package: pkg)
599-
XCTFail("expected an analysisError to be thrown")
600-
} catch let AppError.analysisError(.some(pkgId), msg) {
601-
// validate
602-
XCTAssertEqual(pkgId, .id0)
603-
XCTAssertEqual(msg, "Default branch 'main' does not exist in checkout")
598+
// MUT
599+
do {
600+
_ = try await Analyze.getIncomingVersions(client: app.client, package: pkg)
601+
XCTFail("expected an analysisError to be thrown")
602+
} catch let AppError.analysisError(.some(pkgId), msg) {
603+
// validate
604+
XCTAssertEqual(pkgId, .id0)
605+
XCTAssertEqual(msg, "Default branch 'main' does not exist in checkout")
606+
}
604607
}
605608
}
606609

@@ -624,9 +627,9 @@ class AnalyzerTests: AppTestCase {
624627
func test_diffVersions() async throws {
625628
try await withDependencies {
626629
$0.git.getTags = { @Sendable _ in [.tag(1, 2, 3)] }
630+
$0.git.hasBranch = { @Sendable _, _ in true }
627631
} operation: {
628632
//setup
629-
Current.git.hasBranch = { @Sendable _, _ in true }
630633
Current.git.revisionInfo = { @Sendable ref, _ in
631634
if ref == .branch("main") { return . init(commit: "sha.main", date: .t0) }
632635
if ref == .tag(1, 2, 3) { return .init(commit: "sha.1.2.3", date: .t1) }
@@ -905,10 +908,10 @@ class AnalyzerTests: AppTestCase {
905908
$0.git.commitCount = { @Sendable _ in 12 }
906909
$0.git.firstCommitDate = { @Sendable _ in .t0 }
907910
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(2, 0, 0)] }
911+
$0.git.hasBranch = { @Sendable _, _ in true }
908912
$0.git.lastCommitDate = { @Sendable _ in .t1 }
909913
} operation: {
910914
// setup
911-
Current.git.hasBranch = { @Sendable _, _ in true }
912915
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
913916
Current.git.shortlog = { @Sendable _ in
914917
"""
@@ -1311,6 +1314,7 @@ class AnalyzerTests: AppTestCase {
13111314
$0.git.commitCount = { @Sendable _ in 2 }
13121315
$0.git.firstCommitDate = { @Sendable _ in .t0 }
13131316
$0.git.getTags = { @Sendable _ in throw TestError.unspecifiedError }
1317+
$0.git.hasBranch = { @Sendable _, _ in true }
13141318
$0.git.lastCommitDate = { @Sendable _ in .t1 }
13151319
} operation: {
13161320
let pkgId = UUID()
@@ -1332,7 +1336,6 @@ class AnalyzerTests: AppTestCase {
13321336
latest: .release,
13331337
packageName: "foo-1",
13341338
reference: .tag(1, 0, 0)).save(on: app.db)
1335-
Current.git.hasBranch = { @Sendable _, _ in true }
13361339
Current.git.shortlog = { @Sendable _ in
13371340
"""
13381341
1\tPerson 1
@@ -1419,6 +1422,7 @@ class AnalyzerTests: AppTestCase {
14191422
$0.git.commitCount = { @Sendable _ in 2 }
14201423
$0.git.firstCommitDate = { @Sendable _ in .t0 }
14211424
$0.git.getTags = {@Sendable _ in [.tag(1, 0, 0)] }
1425+
$0.git.hasBranch = { @Sendable _, _ in true }
14221426
$0.git.lastCommitDate = { @Sendable _ in .t1 }
14231427
} operation: {
14241428
let pkgId = UUID()
@@ -1440,7 +1444,6 @@ class AnalyzerTests: AppTestCase {
14401444
latest: .release,
14411445
packageName: "foo-1",
14421446
reference: .tag(1, 0, 0)).save(on: app.db)
1443-
Current.git.hasBranch = { @Sendable _, _ in true }
14441447
struct Error: Swift.Error { }
14451448
Current.git.shortlog = { @Sendable _ in
14461449
"""
@@ -1530,6 +1533,7 @@ class AnalyzerTests: AppTestCase {
15301533
$0.git.commitCount = { @Sendable _ in 12 }
15311534
$0.git.firstCommitDate = { @Sendable _ in .t0 }
15321535
$0.git.getTags = { @Sendable _ in [] }
1536+
$0.git.hasBranch = { @Sendable _, _ in true }
15331537
$0.git.lastCommitDate = { @Sendable _ in .t1 }
15341538
} operation: {
15351539
// setup
@@ -1539,7 +1543,6 @@ class AnalyzerTests: AppTestCase {
15391543
name: "1",
15401544
owner: "foo",
15411545
stars: 100).save(on: app.db)
1542-
Current.git.hasBranch = { @Sendable _, _ in true }
15431546
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha1", date: .t0) }
15441547
Current.git.shortlog = { @Sendable _ in "10\tPerson 1" }
15451548
Current.shell.run = { @Sendable cmd, path in

Tests/AppTests/AnalyzerVersionThrottlingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
191191
try await withDependencies {
192192
$0.date.now = .t0
193193
$0.git.getTags = { @Sendable _ in [.branch("main")] }
194+
$0.git.hasBranch = { @Sendable _, _ in true }
194195
} operation: {
195196
// setup
196-
Current.git.hasBranch = { @Sendable _, _ in true }
197197
let pkg = Package(url: "1".asGithubUrl.url)
198198
try await pkg.save(on: app.db)
199199
try await Repository(package: pkg, defaultBranch: "main").save(on: app.db)
@@ -246,8 +246,8 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
246246
// Leaving tags out of it for simplicity - they are tested specifically
247247
// in test_throttle_ignore_tags above.
248248
$0.git.getTags = { @Sendable _ in [] }
249+
$0.git.hasBranch = { @Sendable _, _ in true }
249250
} operation: {
250-
Current.git.hasBranch = { @Sendable _, _ in true }
251251

252252
// Little helper to simulate minimal version reconciliation
253253
func runVersionReconciliation() async throws -> VersionDelta {

Tests/AppTests/IngestionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ class IngestionTests: AppTestCase {
436436
$0.git.commitCount = { @Sendable _ in 1 }
437437
$0.git.firstCommitDate = { @Sendable _ in .t0 }
438438
$0.git.getTags = { @Sendable _ in [] }
439+
$0.git.hasBranch = { @Sendable _, _ in true }
439440
$0.git.lastCommitDate = { @Sendable _ in .t0 }
440441
} operation: { [db = app.db] in
441-
Current.git.hasBranch = { @Sendable _, _ in true }
442442
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha0", date: .t0) }
443443
Current.git.shortlog = { @Sendable _ in "" }
444444
Current.shell.run = { @Sendable cmd, _ in

Tests/AppTests/MastodonTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class MastodonTests: AppTestCase {
3030
$0.git.commitCount = { @Sendable _ in 12 }
3131
$0.git.firstCommitDate = { @Sendable _ in .t0 }
3232
$0.git.getTags = { @Sendable _ in [Reference.tag(1, 2, 3)] }
33+
$0.git.hasBranch = { @Sendable _, _ in true }
3334
$0.git.lastCommitDate = { @Sendable _ in .t2 }
3435
$0.github.fetchLicense = { @Sendable _, _ in nil }
3536
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
@@ -44,7 +45,6 @@ final class MastodonTests: AppTestCase {
4445
} operation: {
4546
// setup
4647
let url = "https://github.com/foo/bar"
47-
Current.git.hasBranch = { @Sendable _, _ in true }
4848
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
4949
Current.git.shortlog = { @Sendable _ in
5050
"""

Tests/AppTests/Mocks/Git+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
extension Git {
1919
static let mock: Self = .init(
20-
hasBranch: { _, _ in fatalError("not initialized") },
2120
revisionInfo: { _, _ in fatalError("not initialized") },
2221
shortlog: { _ in fatalError("not initialized") }
2322
)

Tests/AppTests/PackageController+routesTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,7 @@ class PackageController_routesTests: SnapshotTestCase {
15081508
$0.git.commitCount = { @Sendable _ in 2}
15091509
$0.git.firstCommitDate = { @Sendable _ in .t0 }
15101510
$0.git.getTags = { @Sendable _ in [] }
1511+
$0.git.hasBranch = { @Sendable _, _ in true }
15111512
$0.git.lastCommitDate = { @Sendable _ in .t1 }
15121513
$0.httpClient.fetchDocumentation = { @Sendable _ in .ok(body: .mockIndexHTML()) }
15131514
$0.timeZone = .utc
@@ -1525,7 +1526,6 @@ class PackageController_routesTests: SnapshotTestCase {
15251526
reference: .branch("main"))
15261527
.save(on: app.db)
15271528
Current.git = .init(
1528-
hasBranch: { _, _ in true },
15291529
revisionInfo: { ref, _ in
15301530
if ref == .branch("main") { return .init(commit: "new-commit", date: .t1) }
15311531
fatalError("revisionInfo: \(ref)")

0 commit comments

Comments
 (0)