Skip to content

Commit

Permalink
fixed FETCH_HEAD parsing (#124)
Browse files Browse the repository at this point in the history
* fixed FETCH_HEAD parsing
* attempt to increase speed of coverage processing
  • Loading branch information
ypopovych authored Aug 21, 2024
1 parent fb750c5 commit e696626
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Sources/DatadogSDKTesting/Coverage/DDCoverageHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class DDCoverageHelper {
Log.debug("DDCoverageHelper location: \(path.url.path)")
initialCoverageSaved = false
coverageWorkQueue = OperationQueue()
coverageWorkQueue.qualityOfService = .background
coverageWorkQueue.maxConcurrentOperationCount = (ProcessInfo.processInfo.activeProcessorCount - 1)
coverageWorkQueue.qualityOfService = .utility
coverageWorkQueue.maxConcurrentOperationCount = max(ProcessInfo.processInfo.activeProcessorCount - 1, 1)
}

func clearCounters() {
Expand Down
56 changes: 41 additions & 15 deletions Sources/DatadogSDKTesting/Utils/GitInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,21 @@ struct GitInfo {
init(gitFolder: URL) throws {
workspacePath = gitFolder.deletingLastPathComponent().path
let headPath = gitFolder.appendingPathComponent("HEAD")
var mergePath: String?
let head = try String(contentsOf: headPath)
let head = try String(contentsOf: headPath).trimmingCharacters(in: .whitespacesAndNewlines)
if head.hasPrefix("ref:") {
mergePath = head.trimmingCharacters(in: .whitespacesAndNewlines)
mergePath!.removeFirst(4)
mergePath = mergePath!.trimmingCharacters(in: .whitespacesAndNewlines)
var mergePath = head
mergePath.removeFirst(4)
mergePath = mergePath.trimmingCharacters(in: .whitespacesAndNewlines)
self.branch = mergePath
let refData = try String(contentsOf: gitFolder.appendingPathComponent(mergePath!))
let refData = try String(contentsOf: gitFolder.appendingPathComponent(mergePath))
commit = refData.trimmingCharacters(in: .whitespacesAndNewlines)
} else {
let fetchHeadPath = gitFolder.appendingPathComponent("FETCH_HEAD")
if let fetchHead = try? String(contentsOf: fetchHeadPath),
let first = fetchHead.firstIndex(of: "'"),
let last = fetchHead[fetchHead.index(after: first)...].firstIndex(of: "'")
{
let auxBranch = fetchHead[fetchHead.index(after: first) ... fetchHead.index(before: last)]
if !auxBranch.isEmpty {
self.branch = String(auxBranch)
}
if let records = FetchHeadRecord.from(file: fetchHeadPath)?
.filter({$0.commit == head && $0.type == "branch"}), records.count > 0 {
self.branch = records.first!.ref
}
commit = head.trimmingCharacters(in: .whitespacesAndNewlines)
commit = head
}

let configPath = gitFolder.appendingPathComponent("config")
Expand Down Expand Up @@ -413,3 +407,35 @@ struct CommitInfo {
fullMessage = message
}
}

extension GitInfo {
struct FetchHeadRecord {
let commit: String
let modifier: String?
let type: String?
let ref: String
let info: String

init?(line: String) {
let parts = line.components(separatedBy: "\t")
guard parts.count == 3 else { return nil }
let refInfo = parts[2]
guard let refStart = refInfo.firstIndex(of: "'"),
let refEnd = refInfo[refInfo.index(after: refStart)...].firstIndex(of: "'") else { return nil }
commit = parts[0]
modifier = parts[1].count > 0 ? parts[1] : nil
type = refStart > refInfo.startIndex ?
refInfo[..<refStart].trimmingCharacters(in: .whitespaces) : nil
ref = String(refInfo[refInfo.index(after: refStart)..<refEnd])
info = String(refInfo[refInfo.index(after: refEnd)...])
}

static func from(file url: URL) -> [Self]? {
(try? String(contentsOf: url)).map { Self.from(contents: $0) }
}

static func from(contents string: String) -> [Self] {
string.components(separatedBy: .newlines).compactMap { .init(line: $0) }
}
}
}
41 changes: 41 additions & 0 deletions Tests/DatadogSDKTesting/Utils/GitInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,45 @@ class GitInfoTests: XCTestCase {
XCTAssertEqual(gitInfo.committerEmail, "[email protected]")
XCTAssertEqual(gitInfo.committerDate, "2021-05-06T08:03:32Z")
}

func testFetchHeadParsing() {
let git = fixturesURL.appendingPathComponent("git")
let records1 = GitInfo.FetchHeadRecord.from(file: git.appendingPathComponent("fetch_head_1"))
XCTAssertNotNil(records1)
XCTAssertEqual(records1?.count, 2)

XCTAssertEqual(records1?[0].commit, "284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef")
XCTAssertNil(records1?[0].modifier)
XCTAssertNil(records1?[0].type)
XCTAssertEqual(records1?[0].ref, "refs/pipelines/42233408")
XCTAssertEqual(records1?[0].info, " of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios")

XCTAssertEqual(records1?[1].commit, "284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef")
XCTAssertNil(records1?[1].modifier)
XCTAssertEqual(records1?[1].type, "branch")
XCTAssertEqual(records1?[1].ref, "ncreated/chore/ci-test-viz-debug-branch-name")
XCTAssertEqual(records1?[1].info, " of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios")

let records2 = GitInfo.FetchHeadRecord.from(file: git.appendingPathComponent("fetch_head_2"))
XCTAssertNotNil(records2)
XCTAssertEqual(records2?.count, 3)

XCTAssertEqual(records2?[0].commit, "2803d13cb33b06e2deb75428f19f59158468e370")
XCTAssertEqual(records2?[0].modifier, "not-for-merge")
XCTAssertEqual(records2?[0].type, "branch")
XCTAssertEqual(records2?[0].ref, "main")
XCTAssertEqual(records2?[0].info, " of github.com:DataDog/dd-sdk-swift-testing")

XCTAssertEqual(records2?[1].commit, "836a91d263748a7ac857d2427f6f19fbc7cdb547")
XCTAssertEqual(records2?[1].modifier, "not-for-merge")
XCTAssertEqual(records2?[1].type, "branch")
XCTAssertEqual(records2?[1].ref, "update-binary")
XCTAssertEqual(records2?[1].info, " of github.com:DataDog/dd-sdk-swift-testing")

XCTAssertEqual(records2?[2].commit, "5cba509698f41d8869253d5cc7966a5ef2d46f0a")
XCTAssertEqual(records2?[2].modifier, "not-for-merge")
XCTAssertEqual(records2?[2].type, "branch")
XCTAssertEqual(records2?[2].ref, "yehor-popovych/manual-api-refactoring")
XCTAssertEqual(records2?[2].info, " of github.com:DataDog/dd-sdk-swift-testing")
}
}
2 changes: 2 additions & 0 deletions Tests/fixtures/git/fetch_head_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef 'refs/pipelines/42233408' of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios
284cd8dcaf3ec6171bbc1565c8ddd8a41fac4eef branch 'ncreated/chore/ci-test-viz-debug-branch-name' of https://gitlab.ddbuild.io/DataDog/dd-sdk-ios
3 changes: 3 additions & 0 deletions Tests/fixtures/git/fetch_head_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2803d13cb33b06e2deb75428f19f59158468e370 not-for-merge branch 'main' of github.com:DataDog/dd-sdk-swift-testing
836a91d263748a7ac857d2427f6f19fbc7cdb547 not-for-merge branch 'update-binary' of github.com:DataDog/dd-sdk-swift-testing
5cba509698f41d8869253d5cc7966a5ef2d46f0a not-for-merge branch 'yehor-popovych/manual-api-refactoring' of github.com:DataDog/dd-sdk-swift-testing

0 comments on commit e696626

Please sign in to comment.