Skip to content

Commit 42c9079

Browse files
Merge pull request #3641 from SwiftPackageIndex/issue-3469-dependency-transition-24
Issue 3469 dependency transition 24
2 parents 5ccd4b2 + a89790d commit 42c9079

17 files changed

+287
-326
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 2 additions & 2 deletions
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

@@ -407,7 +407,7 @@ extension Analyze {
407407
let references = [defaultBranch] + tags
408408
return try await references
409409
.mapAsync { ref in
410-
let revInfo = try await Current.git.revisionInfo(ref, cacheDir)
410+
let revInfo = try await git.revisionInfo(ref, at: cacheDir)
411411
let url = package.model.versionUrl(for: ref)
412412
return try Version(package: package.model,
413413
commit: revInfo.commit,

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import FoundationNetworking
2323

2424

2525
struct AppEnvironment: Sendable {
26-
var git: Git
2726
var logger: @Sendable () -> Logger
2827
var setLogger: @Sendable (Logger) -> Void
2928
var shell: Shell
@@ -34,7 +33,6 @@ extension AppEnvironment {
3433
nonisolated(unsafe) static var logger: Logger!
3534

3635
static let live = AppEnvironment(
37-
git: .live,
3836
logger: { logger },
3937
setLogger: { logger in Self.logger = logger },
4038
shell: .live
@@ -43,19 +41,6 @@ extension AppEnvironment {
4341

4442

4543

46-
struct Git: Sendable {
47-
var hasBranch: @Sendable (Reference, String) async throws -> Bool
48-
var revisionInfo: @Sendable (Reference, String) async throws -> RevisionInfo
49-
var shortlog: @Sendable (String) async throws -> String
50-
51-
static let live: Self = .init(
52-
hasBranch: { ref, path in try await hasBranch(ref, at: path) },
53-
revisionInfo: { ref, path in try await revisionInfo(ref, at: path) },
54-
shortlog: { path in try await shortlog(at: path) }
55-
)
56-
}
57-
58-
5944
struct Shell: Sendable {
6045
var run: @Sendable (ShellOutCommand, String) async throws -> String
6146

Sources/App/Core/Dependencies/GitClient.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ 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
29+
var revisionInfo: @Sendable (Reference, _ at: String) async throws -> Git.RevisionInfo
30+
var shortlog: @Sendable (_ at: String) async throws -> String
2831
}
2932

3033

@@ -34,7 +37,10 @@ extension GitClient: DependencyKey {
3437
commitCount: { path in try await Git.commitCount(at: path) },
3538
firstCommitDate: { path in try await Git.firstCommitDate(at: path) },
3639
getTags: { path in try await Git.getTags(at: path) },
37-
lastCommitDate: { path in try await Git.lastCommitDate(at: path) }
40+
hasBranch: { ref, path in try await Git.hasBranch(ref, at: path) },
41+
lastCommitDate: { path in try await Git.lastCommitDate(at: path) },
42+
revisionInfo: { ref, path in try await Git.revisionInfo(ref, at: path) },
43+
shortlog: { path in try await Git.shortlog(at: path) }
3844
)
3945
}
4046
}

Sources/App/Core/Git.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ import SemanticVersion
1717
import ShellOut
1818

1919

20-
enum GitError: LocalizedError {
21-
case invalidInteger
22-
case invalidTimestamp
23-
case invalidRevisionInfo(String)
24-
}
20+
enum Git {
2521

26-
extension Git {
22+
enum Error: LocalizedError {
23+
case invalidInteger
24+
case invalidTimestamp
25+
case invalidRevisionInfo(String)
26+
}
2727

2828
static func commitCount(at path: String) async throws -> Int {
2929
let res = try await Current.shell.run(command: .gitCommitCount, at: path)
3030
guard let count = Int(res) else {
31-
throw GitError.invalidInteger
31+
throw Error.invalidInteger
3232
}
3333
return count
3434
}
@@ -39,7 +39,7 @@ extension Git {
3939
.trimming { $0 == Character("\"") }
4040
)
4141
guard let timestamp = TimeInterval(res) else {
42-
throw GitError.invalidTimestamp
42+
throw Error.invalidTimestamp
4343
}
4444
return Date(timeIntervalSince1970: timestamp)
4545
}
@@ -50,7 +50,7 @@ extension Git {
5050
.trimming { $0 == Character("\"") }
5151
)
5252
guard let timestamp = TimeInterval(res) else {
53-
throw GitError.invalidTimestamp
53+
throw Error.invalidTimestamp
5454
}
5555
return Date(timeIntervalSince1970: timestamp)
5656
}
@@ -83,10 +83,10 @@ extension Git {
8383
let parts = res.components(separatedBy: separator)
8484
guard parts.count == 2 else {
8585
Current.logger().warning(#"Git.invalidRevisionInfo: \#(res) for '\#(ShellOutCommand.gitRevisionInfo(reference: reference, separator: separator))' at: \#(path)"#)
86-
throw GitError.invalidRevisionInfo(res)
86+
throw Error.invalidRevisionInfo(res)
8787
}
8888
let hash = parts[0]
89-
guard let timestamp = TimeInterval(parts[1]) else { throw GitError.invalidTimestamp }
89+
guard let timestamp = TimeInterval(parts[1]) else { throw Error.invalidTimestamp }
9090
let date = Date(timeIntervalSince1970: timestamp)
9191
return .init(commit: hash, date: date)
9292
}
@@ -99,4 +99,5 @@ extension Git {
9999
let commit: CommitHash
100100
let date: Date
101101
}
102+
102103
}

Sources/App/Core/PackageContributors.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ enum PackageContributors {
6868
/// Gets the git history in a string log
6969
private static func runShortlog(gitCacheDirectoryPath: String, packageID: UUID?) async throws -> String {
7070
@Dependency(\.fileManager) var fileManager
71+
@Dependency(\.git) var git
72+
7173
if fileManager.fileExists(atPath: gitCacheDirectoryPath) == false {
7274
throw AppError.cacheDirectoryDoesNotExist(packageID, gitCacheDirectoryPath)
7375
}
7476

7577
// attempt to shortlog
7678
do {
77-
return try await Current.git.shortlog(gitCacheDirectoryPath)
79+
return try await git.shortlog(gitCacheDirectoryPath)
7880
} catch {
7981
throw AppError.shellCommandFailed("gitShortlog",
8082
gitCacheDirectoryPath,

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +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 }
77-
Current.git.revisionInfo = { @Sendable ref, checkoutDir in
78-
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
79-
if checkoutDir.hasSuffix("foo-2") { return .init(commit: "commit \(ref)", date: .t1) }
80-
throw SetupError()
81-
}
82-
Current.git.shortlog = { @Sendable _ in
83-
"""
84-
1000\tPerson 1
85-
871\tPerson 2
86-
703\tPerson 3
87-
360\tPerson 4
88-
108\tPerson 5
89-
"""
90-
}
91-
9276
Current.shell.run = Self.defaultShellRun
9377
}
9478

@@ -103,7 +87,22 @@ final class AnalyzeErrorTests: AppTestCase {
10387
if checkoutDir.hasSuffix("foo-2") { return [.tag(1, 2, 3)] }
10488
throw SetupError()
10589
}
90+
$0.git.hasBranch = { @Sendable _, _ in true }
10691
$0.git.lastCommitDate = { @Sendable _ in .t1 }
92+
$0.git.revisionInfo = { @Sendable ref, checkoutDir in
93+
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
94+
if checkoutDir.hasSuffix("foo-2") { return .init(commit: "commit \(ref)", date: .t1) }
95+
throw SetupError()
96+
}
97+
$0.git.shortlog = { @Sendable _ in
98+
"""
99+
1000\tPerson 1
100+
871\tPerson 2
101+
703\tPerson 3
102+
360\tPerson 4
103+
108\tPerson 5
104+
"""
105+
}
107106
$0.httpClient.mastodonPost = { @Sendable [socialPosts = self.socialPosts] message in
108107
socialPosts.withValue { $0.append(message) }
109108
}

0 commit comments

Comments
 (0)