Skip to content

Commit be82bd6

Browse files
committed
Move revisionInfo to GitClient
1 parent da50588 commit be82bd6

13 files changed

+148
-151
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ extension AppEnvironment {
4444

4545

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

5049
static let live: Self = .init(
51-
revisionInfo: { ref, path in try await revisionInfo(ref, at: path) },
5250
shortlog: { path in try await shortlog(at: path) }
5351
)
5452
}

Sources/App/Core/Dependencies/GitClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct GitClient {
2626
var getTags: @Sendable (_ at: String) async throws -> [Reference]
2727
var hasBranch: @Sendable (Reference, _ at: String) async throws -> Bool
2828
var lastCommitDate: @Sendable (_ at: String) async throws -> Date
29+
var revisionInfo: @Sendable (Reference, _ at: String) async throws -> Git.RevisionInfo
2930
}
3031

3132

@@ -36,7 +37,8 @@ extension GitClient: DependencyKey {
3637
firstCommitDate: { path in try await Git.firstCommitDate(at: path) },
3738
getTags: { path in try await Git.getTags(at: path) },
3839
hasBranch: { ref, path in try await Git.hasBranch(ref, at: path) },
39-
lastCommitDate: { path in try await Git.lastCommitDate(at: path) }
40+
lastCommitDate: { path in try await Git.lastCommitDate(at: path) },
41+
revisionInfo: { ref, path in try await Git.revisionInfo(ref, at: path) }
4042
)
4143
}
4244
}

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +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.revisionInfo = { @Sendable ref, checkoutDir in
77-
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
78-
if checkoutDir.hasSuffix("foo-2") { return .init(commit: "commit \(ref)", date: .t1) }
79-
throw SetupError()
80-
}
8176
Current.git.shortlog = { @Sendable _ in
8277
"""
8378
1000\tPerson 1
@@ -104,6 +99,11 @@ final class AnalyzeErrorTests: AppTestCase {
10499
}
105100
$0.git.hasBranch = { @Sendable _, _ in true }
106101
$0.git.lastCommitDate = { @Sendable _ in .t1 }
102+
$0.git.revisionInfo = { @Sendable ref, checkoutDir in
103+
if checkoutDir.hasSuffix("foo-1") { return .init(commit: "commit \(ref)", date: .t1) }
104+
if checkoutDir.hasSuffix("foo-2") { return .init(commit: "commit \(ref)", date: .t1) }
105+
throw SetupError()
106+
}
107107
$0.httpClient.mastodonPost = { @Sendable [socialPosts = self.socialPosts] message in
108108
socialPosts.withValue { $0.append(message) }
109109
}

Tests/AppTests/AnalyzerTests.swift

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,22 @@ class AnalyzerTests: AppTestCase {
227227
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(1, 1, 1)] }
228228
$0.git.hasBranch = { @Sendable _, _ in true }
229229
$0.git.lastCommitDate = { @Sendable _ in .t2 }
230+
$0.git.revisionInfo = { @Sendable ref, _ in
231+
// simulate the following scenario:
232+
// - main branch has moved from commit0 -> commit3 (timestamp t3)
233+
// - 1.0.0 has been re-tagged (!) from commit0 -> commit1 (timestamp t1)
234+
// - 1.1.1 has been added at commit2 (timestamp t2)
235+
switch ref {
236+
case _ where ref == .tag(1, 0, 0):
237+
return .init(commit: "commit1", date: .t1)
238+
case _ where ref == .tag(1, 1, 1):
239+
return .init(commit: "commit2", date: .t2)
240+
case .branch("main"):
241+
return .init(commit: "commit3", date: .t3)
242+
default:
243+
fatalError("unexpected reference: \(ref)")
244+
}
245+
}
230246
$0.httpClient.mastodonPost = { @Sendable _ in }
231247
} operation: {
232248
// setup
@@ -251,22 +267,6 @@ class AnalyzerTests: AppTestCase {
251267
packageName: "foo-1",
252268
reference: .tag(1, 0, 0)).save(on: app.db)
253269

254-
Current.git.revisionInfo = { @Sendable ref, _ in
255-
// simulate the following scenario:
256-
// - main branch has moved from commit0 -> commit3 (timestamp t3)
257-
// - 1.0.0 has been re-tagged (!) from commit0 -> commit1 (timestamp t1)
258-
// - 1.1.1 has been added at commit2 (timestamp t2)
259-
switch ref {
260-
case _ where ref == .tag(1, 0, 0):
261-
return .init(commit: "commit1", date: .t1)
262-
case _ where ref == .tag(1, 1, 1):
263-
return .init(commit: "commit2", date: .t2)
264-
case .branch("main"):
265-
return .init(commit: "commit3", date: .t3)
266-
default:
267-
fatalError("unexpected reference: \(ref)")
268-
}
269-
}
270270
Current.git.shortlog = { @Sendable _ in
271271
"""
272272
10\tPerson 1
@@ -366,6 +366,7 @@ class AnalyzerTests: AppTestCase {
366366
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0)] }
367367
$0.git.hasBranch = { @Sendable _, _ in true }
368368
$0.git.lastCommitDate = { @Sendable _ in .t1 }
369+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
369370
} operation: {
370371
// setup
371372
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
@@ -375,7 +376,6 @@ class AnalyzerTests: AppTestCase {
375376
}
376377
let lastUpdate = Date()
377378

378-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
379379
Current.git.shortlog = { @Sendable _ in
380380
"""
381381
10\tPerson 1
@@ -565,9 +565,9 @@ class AnalyzerTests: AppTestCase {
565565
try await withDependencies {
566566
$0.git.getTags = { @Sendable _ in [.tag(1, 2, 3)] }
567567
$0.git.hasBranch = { @Sendable _, _ in true }
568+
$0.git.revisionInfo = { @Sendable ref, _ in .init(commit: "sha-\(ref)", date: .t0) }
568569
} operation: {
569570
// setup
570-
Current.git.revisionInfo = { @Sendable ref, _ in .init(commit: "sha-\(ref)", date: .t0) }
571571
do {
572572
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
573573
try await pkg.save(on: app.db)
@@ -628,13 +628,13 @@ class AnalyzerTests: AppTestCase {
628628
try await withDependencies {
629629
$0.git.getTags = { @Sendable _ in [.tag(1, 2, 3)] }
630630
$0.git.hasBranch = { @Sendable _, _ in true }
631-
} operation: {
632-
//setup
633-
Current.git.revisionInfo = { @Sendable ref, _ in
631+
$0.git.revisionInfo = { @Sendable ref, _ in
634632
if ref == .branch("main") { return . init(commit: "sha.main", date: .t0) }
635633
if ref == .tag(1, 2, 3) { return .init(commit: "sha.1.2.3", date: .t1) }
636634
fatalError("unknown ref: \(ref)")
637635
}
636+
} operation: {
637+
//setup
638638
Current.shell.run = { @Sendable cmd, _ in throw TestError.unknownCommand }
639639
let pkgId = UUID()
640640
do {
@@ -910,9 +910,9 @@ class AnalyzerTests: AppTestCase {
910910
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0), .tag(2, 0, 0)] }
911911
$0.git.hasBranch = { @Sendable _, _ in true }
912912
$0.git.lastCommitDate = { @Sendable _ in .t1 }
913+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
913914
} operation: {
914915
// setup
915-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha", date: .t0) }
916916
Current.git.shortlog = { @Sendable _ in
917917
"""
918918
10\tPerson 1
@@ -1344,9 +1344,9 @@ class AnalyzerTests: AppTestCase {
13441344
}
13451345
Current.shell.run = { @Sendable cmd, path in "" }
13461346

1347-
do { // first scenario: bad getTags
1348-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "", date: .t1) }
1349-
1347+
try await withDependencies { // first scenario: bad getTags
1348+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "", date: .t1) }
1349+
} operation: {
13501350
// MUT
13511351
try await Analyze.analyze(client: app.client,
13521352
database: app.db,
@@ -1361,9 +1361,8 @@ class AnalyzerTests: AppTestCase {
13611361

13621362
try await withDependencies { // second scenario: revisionInfo throws
13631363
$0.git.getTags = { @Sendable _ in [.tag(1, 0, 0)] }
1364+
$0.git.revisionInfo = { @Sendable _, _ in throw TestError.unspecifiedError }
13641365
} operation: {
1365-
Current.git.revisionInfo = { @Sendable _, _ in throw TestError.unspecifiedError }
1366-
13671366
// MUT
13681367
try await Analyze.analyze(client: app.client,
13691368
database: app.db,
@@ -1378,9 +1377,8 @@ class AnalyzerTests: AppTestCase {
13781377

13791378
try await withDependencies { // second scenario: gitTags throws
13801379
$0.git.getTags = { @Sendable _ in throw TestError.unspecifiedError }
1380+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "", date: .t1) }
13811381
} operation: {
1382-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "", date: .t1) }
1383-
13841382
// MUT
13851383
try await Analyze.analyze(client: app.client,
13861384
database: app.db,
@@ -1395,8 +1393,8 @@ class AnalyzerTests: AppTestCase {
13951393

13961394
try await withDependencies { // third scenario: everything throws
13971395
$0.git.getTags = { @Sendable _ in throw TestError.unspecifiedError }
1396+
$0.git.revisionInfo = { @Sendable _, _ in throw TestError.unspecifiedError }
13981397
} operation: {
1399-
Current.git.revisionInfo = { @Sendable _, _ in throw TestError.unspecifiedError }
14001398
Current.shell.run = { @Sendable _, _ in throw TestError.unspecifiedError }
14011399

14021400
// MUT
@@ -1453,8 +1451,8 @@ class AnalyzerTests: AppTestCase {
14531451
}
14541452
Current.shell.run = { @Sendable cmd, path in return "" }
14551453

1456-
do { // ensure happy path passes test (no revision changes)
1457-
Current.git.revisionInfo = { @Sendable ref, _ in
1454+
try await withDependencies { // ensure happy path passes test (no revision changes)
1455+
$0.git.revisionInfo = { @Sendable ref, _ in
14581456
switch ref {
14591457
case .tag(.init(1, 0, 0), "1.0.0"):
14601458
return .init(commit: "commit0", date: .t0)
@@ -1464,7 +1462,7 @@ class AnalyzerTests: AppTestCase {
14641462
throw Error()
14651463
}
14661464
}
1467-
1465+
} operation: {
14681466
// MUT
14691467
try await Analyze.analyze(client: app.client,
14701468
database: app.db,
@@ -1482,8 +1480,8 @@ class AnalyzerTests: AppTestCase {
14821480
pkg.processingStage = .ingestion
14831481
try await pkg.save(on: app.db)
14841482

1485-
do { // simulate "main" branch moving forward to ("commit0", .t1)
1486-
Current.git.revisionInfo = { @Sendable ref, _ in
1483+
try await withDependencies { // simulate "main" branch moving forward to ("commit0", .t1)
1484+
$0.git.revisionInfo = { @Sendable ref, _ in
14871485
switch ref {
14881486
case .tag(.init(1, 0, 0), "1.0.0"):
14891487
return .init(commit: "commit0", date: .t0)
@@ -1494,6 +1492,7 @@ class AnalyzerTests: AppTestCase {
14941492
throw Error()
14951493
}
14961494
}
1495+
} operation: {
14971496
Current.shell.run = { @Sendable cmd, path in
14981497
// simulate error in getPackageInfo by failing checkout
14991498
if cmd == .gitCheckout(branch: "main") {
@@ -1535,6 +1534,7 @@ class AnalyzerTests: AppTestCase {
15351534
$0.git.getTags = { @Sendable _ in [] }
15361535
$0.git.hasBranch = { @Sendable _, _ in true }
15371536
$0.git.lastCommitDate = { @Sendable _ in .t1 }
1537+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha1", date: .t0) }
15381538
} operation: {
15391539
// setup
15401540
let pkg = try await savePackage(on: app.db, id: .id0, "https://github.com/foo/1".url, processingStage: .ingestion)
@@ -1543,7 +1543,6 @@ class AnalyzerTests: AppTestCase {
15431543
name: "1",
15441544
owner: "foo",
15451545
stars: 100).save(on: app.db)
1546-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha1", date: .t0) }
15471546
Current.git.shortlog = { @Sendable _ in "10\tPerson 1" }
15481547
Current.shell.run = { @Sendable cmd, path in
15491548
if cmd == .swiftDumpPackage { return .packageDump(name: "foo1") }
@@ -1576,15 +1575,16 @@ class AnalyzerTests: AppTestCase {
15761575
XCTAssertEqual(pkg?.scoreDetails?.numberOfDependencies, 1)
15771576
}
15781577

1579-
// now we simulate a new version on the default branch
1580-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha2", date: .t1) }
1581-
1582-
// third analysis pass
1583-
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(.id0))
1584-
do { // validate
1585-
let pkg = try await Package.query(on: app.db).first()
1586-
// numberOfDependencies must be preserved as 1, even though we've not built this version yet
1587-
XCTAssertEqual(pkg?.scoreDetails?.numberOfDependencies, 1)
1578+
try await withDependencies { // now we simulate a new version on the default branch
1579+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha2", date: .t1) }
1580+
} operation: {
1581+
// third analysis pass
1582+
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(.id0))
1583+
do { // validate
1584+
let pkg = try await Package.query(on: app.db).first()
1585+
// numberOfDependencies must be preserved as 1, even though we've not built this version yet
1586+
XCTAssertEqual(pkg?.scoreDetails?.numberOfDependencies, 1)
1587+
}
15881588
}
15891589
}
15901590
}

Tests/AppTests/AnalyzerVersionThrottlingTests.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
201201
try await old.save(on: app.db)
202202
let jpr = try await Package.fetchCandidate(app.db, id: pkg.id!)
203203

204-
do { // keep old version if too soon
205-
Current.git.revisionInfo = { @Sendable _, _ in
204+
try await withDependencies { // keep old version if too soon
205+
$0.git.revisionInfo = { @Sendable _, _ in
206206
.init(commit: "sha_new", date: .t0.addingTimeInterval(.hours(-1)) )
207207
}
208-
208+
} operation: {
209209
// MUT
210210
let res = try await Analyze.diffVersions(client: app.client,
211211
transaction: app.db,
@@ -219,13 +219,12 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
219219

220220
try await withDependencies {
221221
$0.date.now = .t0.addingTimeInterval(.hours(2))
222-
} operation: {
223222
// new version must come through
224-
Current.git.revisionInfo = { @Sendable _, _ in
223+
$0.git.revisionInfo = { @Sendable _, _ in
225224
// now simulate a newer branch revision
226225
.init(commit: "sha_new2", date: .t0.addingTimeInterval(.hours(2)) )
227226
}
228-
227+
} operation: {
229228
// MUT
230229
let res = try await Analyze.diffVersions(client: app.client,
231230
transaction: app.db,
@@ -279,10 +278,9 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
279278

280279
try await withDependencies {
281280
$0.date.now = commitDates[0]
282-
} operation: {
283281
// start with a branch revision
284-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha0", date: commitDates[0] ) }
285-
282+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha0", date: commitDates[0] ) }
283+
} operation: {
286284
let delta = try await runVersionReconciliation()
287285
XCTAssertEqual(delta.toAdd.map(\.commit), ["sha0"])
288286
XCTAssertEqual(delta.toDelete, [])
@@ -291,10 +289,9 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
291289

292290
try await withDependencies {
293291
$0.date.now = commitDates[1]
294-
} operation: {
295292
// one hour later a new commit landed - which should be ignored
296-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha1", date: commitDates[1] ) }
297-
293+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha1", date: commitDates[1] ) }
294+
} operation: {
298295
let delta = try await runVersionReconciliation()
299296
XCTAssertEqual(delta.toAdd, [])
300297
XCTAssertEqual(delta.toDelete, [])
@@ -305,9 +302,8 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
305302
for idx in 2...6 {
306303
try await withDependencies {
307304
$0.date.now = commitDates[idx]
305+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha\(idx)", date: commitDates[idx] ) }
308306
} operation: {
309-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha\(idx)", date: commitDates[idx] ) }
310-
311307
let delta = try await runVersionReconciliation()
312308
XCTAssertEqual(delta.toAdd, [])
313309
XCTAssertEqual(delta.toDelete, [])
@@ -317,10 +313,9 @@ class AnalyzerVersionThrottlingTests: AppTestCase {
317313

318314
try await withDependencies {
319315
$0.date.now = commitDates[7]
320-
} operation: {
321316
// advancing another 4 hours for a total of 25 hours should finally create a new version
322-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha7", date: commitDates[7] ) }
323-
317+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha7", date: commitDates[7] ) }
318+
} operation: {
324319
let delta = try await runVersionReconciliation()
325320
XCTAssertEqual(delta.toAdd.map(\.commit), ["sha7"])
326321
XCTAssertEqual(delta.toDelete.map(\.commit), ["sha0"])

Tests/AppTests/IngestionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ class IngestionTests: AppTestCase {
438438
$0.git.getTags = { @Sendable _ in [] }
439439
$0.git.hasBranch = { @Sendable _, _ in true }
440440
$0.git.lastCommitDate = { @Sendable _ in .t0 }
441+
$0.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha0", date: .t0) }
441442
} operation: { [db = app.db] in
442-
Current.git.revisionInfo = { @Sendable _, _ in .init(commit: "sha0", date: .t0) }
443443
Current.git.shortlog = { @Sendable _ in "" }
444444
Current.shell.run = { @Sendable cmd, _ in
445445
if cmd.description.hasSuffix("package dump-package") {

0 commit comments

Comments
 (0)