Skip to content

Commit d7e4fca

Browse files
Merge pull request #3638 from SwiftPackageIndex/issue-3469-dependency-transition-23
Issue 3469 dependency transition 23
2 parents e6c7197 + 7f45e6f commit d7e4fca

13 files changed

+282
-225
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,19 @@ 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
338-
repo.firstCommitDate = try? await Current.git.firstCommitDate(gitDirectory)
339-
repo.lastCommitDate = try? await Current.git.lastCommitDate(gitDirectory)
339+
repo.commitCount = (try? await git.commitCount(at: gitDirectory)) ?? 0
340+
repo.firstCommitDate = try? await git.firstCommitDate(at: gitDirectory)
341+
repo.lastCommitDate = try? await git.lastCommitDate(at: gitDirectory)
340342
repo.authors = try? await PackageContributors.extract(gitCacheDirectoryPath: gitDirectory, packageID: package.model.id)
341343

342344
try await repo.update(on: database)
@@ -384,6 +386,8 @@ extension Analyze {
384386
static func getIncomingVersions(client: Client,
385387
package: Joined<Package, Repository>) async throws -> [Version] {
386388
@Dependency(\.fileManager) var fileManager
389+
@Dependency(\.git) var git
390+
387391
guard let cacheDir = fileManager.cacheDirectoryPath(for: package.model) else {
388392
throw AppError.invalidPackageCachePath(package.model.id, package.model.url)
389393
}
@@ -398,7 +402,7 @@ extension Analyze {
398402
throw AppError.analysisError(package.model.id, "Default branch '\(defaultBranch)' does not exist in checkout")
399403
}
400404

401-
let tags = try await Current.git.getTags(cacheDir)
405+
let tags = try await git.getTags(at: cacheDir)
402406

403407
let references = [defaultBranch] + tags
404408
return try await references

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,11 @@ extension AppEnvironment {
4444

4545

4646
struct Git: Sendable {
47-
var commitCount: @Sendable (String) async throws -> Int
48-
var firstCommitDate: @Sendable (String) async throws -> Date
49-
var lastCommitDate: @Sendable (String) async throws -> Date
50-
var getTags: @Sendable (String) async throws -> [Reference]
5147
var hasBranch: @Sendable (Reference, String) async throws -> Bool
5248
var revisionInfo: @Sendable (Reference, String) async throws -> RevisionInfo
5349
var shortlog: @Sendable (String) async throws -> String
5450

5551
static let live: Self = .init(
56-
commitCount: { path in try await commitCount(at: path) },
57-
firstCommitDate: { path in try await firstCommitDate(at: path) },
58-
lastCommitDate: { path in try await lastCommitDate(at: path) },
59-
getTags: { path in try await getTags(at: path) },
6052
hasBranch: { ref, path in try await hasBranch(ref, at: path) },
6153
revisionInfo: { ref, path in try await revisionInfo(ref, at: path) },
6254
shortlog: { path in try await shortlog(at: path) }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
import Dependencies
18+
import DependenciesMacros
19+
import IssueReporting
20+
21+
22+
@DependencyClient
23+
struct GitClient {
24+
var commitCount: @Sendable (_ at: String) async throws -> Int
25+
var firstCommitDate: @Sendable (_ at: String) async throws -> Date
26+
var getTags: @Sendable (_ at: String) async throws -> [Reference]
27+
var lastCommitDate: @Sendable (_ at: String) async throws -> Date
28+
}
29+
30+
31+
extension GitClient: DependencyKey {
32+
static var liveValue: Self {
33+
.init(
34+
commitCount: { path in try await Git.commitCount(at: path) },
35+
firstCommitDate: { path in try await Git.firstCommitDate(at: path) },
36+
getTags: { path in try await Git.getTags(at: path) },
37+
lastCommitDate: { path in try await Git.lastCommitDate(at: path) }
38+
)
39+
}
40+
}
41+
42+
43+
extension GitClient: TestDependencyKey {
44+
static var testValue: Self { .init() }
45+
}
46+
47+
48+
extension DependencyValues {
49+
var git: GitClient {
50+
get { self[GitClient.self] }
51+
set { self[GitClient.self] = newValue }
52+
}
53+
}

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +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 }
77-
Current.git.firstCommitDate = { @Sendable _ in .t0 }
78-
Current.git.lastCommitDate = { @Sendable _ in .t1 }
79-
Current.git.getTags = { @Sendable checkoutDir in
80-
if checkoutDir.hasSuffix("foo-1") { return [] }
81-
if checkoutDir.hasSuffix("foo-2") { return [.tag(1, 2, 3)] }
82-
throw SetupError()
83-
}
8476
Current.git.hasBranch = { @Sendable _, _ in true }
8577
Current.git.revisionInfo = { @Sendable ref, checkoutDir in
8678
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
@@ -104,6 +96,14 @@ final class AnalyzeErrorTests: AppTestCase {
10496
withDependencies {
10597
$0.date.now = .t0
10698
$0.environment.allowSocialPosts = { true }
99+
$0.git.commitCount = { @Sendable _ in 1 }
100+
$0.git.firstCommitDate = { @Sendable _ in .t0 }
101+
$0.git.getTags = { @Sendable checkoutDir in
102+
if checkoutDir.hasSuffix("foo-1") { return [] }
103+
if checkoutDir.hasSuffix("foo-2") { return [.tag(1, 2, 3)] }
104+
throw SetupError()
105+
}
106+
$0.git.lastCommitDate = { @Sendable _ in .t1 }
107107
$0.httpClient.mastodonPost = { @Sendable [socialPosts = self.socialPosts] message in
108108
socialPosts.withValue { $0.append(message) }
109109
}

0 commit comments

Comments
 (0)