Skip to content

Commit 3fa3540

Browse files
committed
Move commitCount to GitClient
1 parent c0148b9 commit 3fa3540

File tree

12 files changed

+29
-27
lines changed

12 files changed

+29
-27
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,17 @@ extension Analyze {
326326
/// - package: `Package` to update
327327
/// - Returns: result future
328328
static func updateRepository(on database: Database, package: Joined<Package, Repository>) async throws {
329+
@Dependency(\.fileManager) var fileManager
330+
@Dependency(\.git) var git
331+
329332
guard let repo = package.repository else {
330333
throw AppError.genericError(package.model.id, "updateRepository: no repository")
331334
}
332-
@Dependency(\.fileManager) var fileManager
333335
guard let gitDirectory = fileManager.cacheDirectoryPath(for: package.model) else {
334336
throw AppError.invalidPackageCachePath(package.model.id, package.model.url)
335337
}
336338

337-
repo.commitCount = (try? await Current.git.commitCount(gitDirectory)) ?? 0
339+
repo.commitCount = (try? await git.commitCount(gitDirectory)) ?? 0
338340
repo.firstCommitDate = try? await Current.git.firstCommitDate(gitDirectory)
339341
repo.lastCommitDate = try? await Current.git.lastCommitDate(gitDirectory)
340342
repo.authors = try? await PackageContributors.extract(gitCacheDirectoryPath: gitDirectory, packageID: package.model.id)

Sources/App/Core/AppEnvironment.swift

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

4545

4646
struct Git: Sendable {
47-
var commitCount: @Sendable (String) async throws -> Int
4847
var firstCommitDate: @Sendable (String) async throws -> Date
4948
var lastCommitDate: @Sendable (String) async throws -> Date
5049
var getTags: @Sendable (String) async throws -> [Reference]
@@ -53,7 +52,6 @@ struct Git: Sendable {
5352
var shortlog: @Sendable (String) async throws -> String
5453

5554
static let live: Self = .init(
56-
commitCount: { path in try await commitCount(at: path) },
5755
firstCommitDate: { path in try await firstCommitDate(at: path) },
5856
lastCommitDate: { path in try await lastCommitDate(at: path) },
5957
getTags: { path in try await getTags(at: path) },

Sources/App/Core/Dependencies/GitClient.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import IssueReporting
1919

2020
@DependencyClient
2121
struct GitClient {
22-
22+
var commitCount: @Sendable (_ at: String) async throws -> Int
2323
}
2424

2525

2626
extension GitClient: DependencyKey {
2727
static var liveValue: Self {
28-
.init()
28+
.init(
29+
commitCount: { path in try await Git.commitCount(at: path) }
30+
)
2931
}
3032
}
3133

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.commitCount = { @Sendable _ in 1 }
7776
Current.git.firstCommitDate = { @Sendable _ in .t0 }
7877
Current.git.lastCommitDate = { @Sendable _ in .t1 }
7978
Current.git.getTags = { @Sendable checkoutDir in
@@ -104,6 +103,7 @@ final class AnalyzeErrorTests: AppTestCase {
104103
withDependencies {
105104
$0.date.now = .t0
106105
$0.environment.allowSocialPosts = { true }
106+
$0.git.commitCount = { @Sendable _ in 1 }
107107
$0.httpClient.mastodonPost = { @Sendable [socialPosts = self.socialPosts] message in
108108
socialPosts.withValue { $0.append(message) }
109109
}

Tests/AppTests/AnalyzerTests.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class AnalyzerTests: AppTestCase {
5656
if path.hasSuffix("Package.resolved") { return true }
5757
return false
5858
}
59+
$0.git = .liveValue
5960
$0.httpClient.mastodonPost = { @Sendable _ in }
6061
} operation: {
6162
// setup
@@ -221,6 +222,7 @@ class AnalyzerTests: AppTestCase {
221222
$0.environment.allowSocialPosts = { true }
222223
$0.environment.loadSPIManifest = { _ in nil }
223224
$0.fileManager.fileExists = { @Sendable _ in true }
225+
$0.git.commitCount = { @Sendable _ in 12 }
224226
$0.httpClient.mastodonPost = { @Sendable _ in }
225227
} operation: {
226228
// setup
@@ -245,7 +247,6 @@ class AnalyzerTests: AppTestCase {
245247
packageName: "foo-1",
246248
reference: .tag(1, 0, 0)).save(on: app.db)
247249

248-
Current.git.commitCount = { @Sendable _ in 12 }
249250
Current.git.firstCommitDate = { @Sendable _ in .t0 }
250251
Current.git.lastCommitDate = { @Sendable _ in .t2 }
251252
Current.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(1, 1, 1)] }
@@ -315,14 +316,14 @@ class AnalyzerTests: AppTestCase {
315316
try await withDependencies {
316317
$0.date.now = .now
317318
$0.fileManager.fileExists = { @Sendable _ in true }
319+
$0.git.commitCount = { @Sendable _ in 12 }
318320
} operation: {
319321
// setup
320322
do {
321323
let pkg = try await savePackage(on: app.db, "https://github.com/foo/1", processingStage: .ingestion)
322324
try await Repository(package: pkg, defaultBranch: "main").save(on: app.db)
323325
}
324326

325-
Current.git.commitCount = { @Sendable _ in 12 }
326327
Current.git.firstCommitDate = { @Sendable _ in .t0 }
327328
Current.git.lastCommitDate = { @Sendable _ in .t1 }
328329
Current.git.hasBranch = { @Sendable _, _ in false } // simulate analysis error via branch mismatch
@@ -360,6 +361,7 @@ class AnalyzerTests: AppTestCase {
360361
$0.environment.allowSocialPosts = { true }
361362
$0.environment.loadSPIManifest = { _ in nil }
362363
$0.fileManager.fileExists = { @Sendable _ in true }
364+
$0.git.commitCount = { @Sendable _ in 12 }
363365
} operation: {
364366
// setup
365367
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
@@ -369,7 +371,6 @@ class AnalyzerTests: AppTestCase {
369371
}
370372
let lastUpdate = Date()
371373

372-
Current.git.commitCount = { @Sendable _ in 12 }
373374
Current.git.firstCommitDate = { @Sendable _ in .t0 }
374375
Current.git.lastCommitDate = { @Sendable _ in .t1 }
375376
Current.git.getTags = { @Sendable _ in [.tag(1, 0, 0)] }
@@ -418,6 +419,7 @@ class AnalyzerTests: AppTestCase {
418419
if path.hasSuffix("Package.swift") { return true }
419420
return false
420421
}
422+
$0.git = .liveValue
421423
} operation: {
422424
// setup
423425
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
@@ -521,9 +523,9 @@ class AnalyzerTests: AppTestCase {
521523
func test_updateRepository() async throws {
522524
try await withDependencies {
523525
$0.fileManager.fileExists = { @Sendable _ in true }
526+
$0.git.commitCount = { @Sendable _ in 12 }
524527
} operation: {
525528
// setup
526-
Current.git.commitCount = { @Sendable _ in 12 }
527529
Current.git.firstCommitDate = { @Sendable _ in .t0 }
528530
Current.git.lastCommitDate = { @Sendable _ in .t1 }
529531
Current.git.shortlog = { @Sendable _ in
@@ -894,9 +896,9 @@ class AnalyzerTests: AppTestCase {
894896
$0.environment.allowSocialPosts = { true }
895897
$0.environment.loadSPIManifest = { _ in nil }
896898
$0.fileManager.fileExists = { @Sendable _ in true }
899+
$0.git.commitCount = { @Sendable _ in 12 }
897900
} operation: {
898901
// setup
899-
Current.git.commitCount = { @Sendable _ in 12 }
900902
Current.git.firstCommitDate = { @Sendable _ in .t0 }
901903
Current.git.lastCommitDate = { @Sendable _ in .t1 }
902904
Current.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(2, 0, 0)] }
@@ -1300,6 +1302,7 @@ class AnalyzerTests: AppTestCase {
13001302
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2571
13011303
try await withDependencies {
13021304
$0.fileManager.fileExists = { @Sendable _ in true }
1305+
$0.git.commitCount = { @Sendable _ in 2 }
13031306
} operation: {
13041307
let pkgId = UUID()
13051308
let pkg = Package(id: pkgId, url: "1".asGithubUrl.url, processingStage: .ingestion)
@@ -1320,7 +1323,6 @@ class AnalyzerTests: AppTestCase {
13201323
latest: .release,
13211324
packageName: "foo-1",
13221325
reference: .tag(1, 0, 0)).save(on: app.db)
1323-
Current.git.commitCount = { @Sendable _ in 2 }
13241326
Current.git.firstCommitDate = { @Sendable _ in .t0 }
13251327
Current.git.hasBranch = { @Sendable _, _ in true }
13261328
Current.git.lastCommitDate = { @Sendable _ in .t1 }
@@ -1406,6 +1408,7 @@ class AnalyzerTests: AppTestCase {
14061408
try await withDependencies {
14071409
$0.date.now = .now
14081410
$0.fileManager.fileExists = { @Sendable _ in true }
1411+
$0.git.commitCount = { @Sendable _ in 2 }
14091412
} operation: {
14101413
let pkgId = UUID()
14111414
let pkg = Package(id: pkgId, url: "1".asGithubUrl.url, processingStage: .ingestion)
@@ -1426,7 +1429,6 @@ class AnalyzerTests: AppTestCase {
14261429
latest: .release,
14271430
packageName: "foo-1",
14281431
reference: .tag(1, 0, 0)).save(on: app.db)
1429-
Current.git.commitCount = { @Sendable _ in 2 }
14301432
Current.git.firstCommitDate = { @Sendable _ in .t0 }
14311433
Current.git.hasBranch = { @Sendable _, _ in true }
14321434
Current.git.lastCommitDate = { @Sendable _ in .t1 }
@@ -1517,6 +1519,7 @@ class AnalyzerTests: AppTestCase {
15171519
$0.date.now = .now
15181520
$0.environment.loadSPIManifest = { _ in nil }
15191521
$0.fileManager.fileExists = { @Sendable _ in true }
1522+
$0.git.commitCount = { @Sendable _ in 12 }
15201523
} operation: {
15211524
// setup
15221525
let pkg = try await savePackage(on: app.db, id: .id0, "https://github.com/foo/1".url, processingStage: .ingestion)
@@ -1525,7 +1528,6 @@ class AnalyzerTests: AppTestCase {
15251528
name: "1",
15261529
owner: "foo",
15271530
stars: 100).save(on: app.db)
1528-
Current.git.commitCount = { @Sendable _ in 12 }
15291531
Current.git.getTags = { @Sendable _ in [] }
15301532
Current.git.hasBranch = { @Sendable _, _ in true }
15311533
Current.git.firstCommitDate = { @Sendable _ in .t0 }

Tests/AppTests/IngestionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ class IngestionTests: AppTestCase {
433433
$0.environment.allowSocialPosts = { false }
434434
$0.environment.loadSPIManifest = { _ in nil }
435435
$0.fileManager.fileExists = { @Sendable _ in true }
436+
$0.git.commitCount = { @Sendable _ in 1 }
436437
} operation: { [db = app.db] in
437-
Current.git.commitCount = { @Sendable _ in 1 }
438438
Current.git.firstCommitDate = { @Sendable _ in .t0 }
439439
Current.git.lastCommitDate = { @Sendable _ in .t0 }
440440
Current.git.getTags = { @Sendable _ in [] }

Tests/AppTests/MastodonTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class MastodonTests: AppTestCase {
2727
$0.environment.allowSocialPosts = { true }
2828
$0.environment.loadSPIManifest = { _ in nil }
2929
$0.fileManager.fileExists = { @Sendable _ in true }
30+
$0.git.commitCount = { @Sendable _ in 12 }
3031
$0.github.fetchLicense = { @Sendable _, _ in nil }
3132
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
3233
$0.github.fetchReadme = { @Sendable _, _ in nil }
@@ -40,7 +41,6 @@ final class MastodonTests: AppTestCase {
4041
} operation: {
4142
// setup
4243
let url = "https://github.com/foo/bar"
43-
Current.git.commitCount = { @Sendable _ in 12 }
4444
Current.git.firstCommitDate = { @Sendable _ in .t0 }
4545
Current.git.lastCommitDate = { @Sendable _ in .t2 }
4646
Current.git.getTags = { @Sendable _ in [Reference.tag(1, 2, 3)] }

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-
commitCount: { _ in fatalError("not initialized") },
2120
firstCommitDate: { _ in fatalError("not initialized") },
2221
lastCommitDate: { _ in fatalError("not initialized") },
2322
getTags: { _ in fatalError("not initialized") },

Tests/AppTests/PackageController+routesTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,7 @@ class PackageController_routesTests: SnapshotTestCase {
15051505
if path.hasSuffix("Package.resolved") { return false }
15061506
return true
15071507
}
1508+
$0.git.commitCount = { @Sendable _ in 2}
15081509
$0.httpClient.fetchDocumentation = { @Sendable _ in .ok(body: .mockIndexHTML()) }
15091510
$0.timeZone = .utc
15101511
} operation: {
@@ -1521,7 +1522,6 @@ class PackageController_routesTests: SnapshotTestCase {
15211522
reference: .branch("main"))
15221523
.save(on: app.db)
15231524
Current.git = .init(
1524-
commitCount: { _ in 2 },
15251525
firstCommitDate: { _ in .t0 },
15261526
lastCommitDate: { _ in .t1 },
15271527
getTags: { _ in [] },

Tests/AppTests/PackageTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ final class PackageTests: AppTestCase {
312312
try await withDependencies {
313313
$0.date.now = .now
314314
$0.fileManager.fileExists = { @Sendable _ in true }
315+
$0.git.commitCount = { @Sendable _ in 12 }
315316
$0.github.fetchLicense = { @Sendable _, _ in nil }
316317
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
317318
$0.github.fetchReadme = { @Sendable _, _ in nil }
@@ -320,7 +321,6 @@ final class PackageTests: AppTestCase {
320321
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
321322
} operation: {
322323
// setup
323-
Current.git.commitCount = { @Sendable _ in 12 }
324324
Current.git.firstCommitDate = { @Sendable _ in Date(timeIntervalSince1970: 0) }
325325
Current.git.getTags = { @Sendable _ in [] }
326326
Current.git.hasBranch = { @Sendable _, _ in true }

0 commit comments

Comments
 (0)