Skip to content

Commit 45eb8e5

Browse files
authored
Symbol graph date (#371)
1 parent a501c00 commit 45eb8e5

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

Sources/SymbolGraphBuilder/Builds/SSGC.Checkout.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SHA1
22
import Symbols
33
import System_
4+
import UnixTime
45

56
extension SSGC
67
{
@@ -9,12 +10,14 @@ extension SSGC
910
/// Absolute path to the checkout directory.
1011
let location:FilePath.Directory
1112
let revision:SHA1
13+
let date:UnixMillisecond
1214

1315
private
14-
init(location:FilePath.Directory, revision:SHA1)
16+
init(location:FilePath.Directory, revision:SHA1, date:UnixMillisecond)
1517
{
1618
self.location = location
1719
self.revision = revision
20+
self.date = date
1821
}
1922
}
2023
}
@@ -78,23 +81,42 @@ extension SSGC.Checkout
7881
try? readable.close()
7982
}
8083

84+
// Get the SHA-1 hash of the current commit
8185
try SystemProcess.init(command: "git", "-C", "\(clone)",
8286
"rev-list", "-n", "1", reference,
8387
stdout: writable)()
8488

85-
// Note: output contains trailing newline
86-
let stdout:String = try .init(unsafeUninitializedCapacity: 64)
89+
let revisionLine:String = try .init(unsafeUninitializedCapacity: 64)
8790
{
8891
try readable.read(into: .init($0))
8992
}
9093

94+
// Get the timestamp of the current commit, in seconds since the Unix epoch.
95+
// 64 bytes should be enough for any Unix timestamp.
96+
try SystemProcess.init(command: "git", "-C", "\(clone)",
97+
"log", "-1", "--format=%ct",
98+
stdout: writable)()
99+
100+
let unixSecondLine:String = try .init(unsafeUninitializedCapacity: 64)
101+
{
102+
try readable.read(into: .init($0))
103+
}
104+
105+
// Note: output lines contains trailing newline
106+
guard
107+
let revision:SHA1 = .init(revisionLine.prefix(while: { !$0.isNewline }))
108+
else
109+
{
110+
fatalError("Could not parse revision from git output: \(revisionLine)")
111+
}
112+
91113
guard
92-
let revision:SHA1 = .init(stdout.prefix(while: \.isHexDigit))
114+
let unixSecond:Int64 = .init(unixSecondLine.prefix(while: { !$0.isNewline }))
93115
else
94116
{
95-
fatalError("unimplemented")
117+
fatalError("Could not parse date from git output: \(unixSecondLine)")
96118
}
97119

98-
return .init(location: clone, revision: revision)
120+
return .init(location: clone, revision: revision, date: .init(index: 1000 * unixSecond))
99121
}
100122
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PackageMetadata
22
import SymbolGraphs
33
import Symbols
4+
import UnixTime
45

56
extension SSGC.PackageBuild
67
{
@@ -10,36 +11,36 @@ extension SSGC.PackageBuild
1011
/// An unversioned SwiftPM build.
1112
case unversioned(Symbol.Package)
1213
/// A versioned SwiftPM build.
13-
case versioned(SPM.DependencyPin, reference:String?)
14+
case versioned(SPM.DependencyPin, ref:String, date:UnixMillisecond)
1415
}
1516
}
1617
extension SSGC.PackageBuild.ID
1718
{
1819
var commit:SymbolGraphMetadata.Commit?
1920
{
20-
guard case .versioned(let pin, let ref?) = self
21+
guard case .versioned(let pin, ref: let name, date: let date) = self
2122
else
2223
{
2324
return nil
2425
}
2526

26-
return .init(name: ref, sha1: pin.revision)
27+
return .init(name: name, sha1: pin.revision, date: date)
2728
}
2829

2930
var package:Symbol.Package
3031
{
3132
switch self
3233
{
33-
case .unversioned(let id): id
34-
case .versioned(let pin, _): pin.identity
34+
case .unversioned(let id): id
35+
case .versioned(let pin, _, _): pin.identity
3536
}
3637
}
3738
var pin:SPM.DependencyPin?
3839
{
3940
switch self
4041
{
4142
case .unversioned: nil
42-
case .versioned(let pin, _): pin
43+
case .versioned(let pin, _, _): pin
4344
}
4445
}
4546
}

Sources/SymbolGraphBuilder/Builds/SSGC.PackageBuild.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,15 @@ extension SSGC.PackageBuild
160160
public static
161161
func remote(project projectName:Symbol.Package,
162162
from repository:String,
163-
at reference:String,
163+
at refName:String,
164164
as type:SSGC.ProjectType = .package,
165165
in workspace:SSGC.Workspace,
166166
flags:Flags = .init(),
167167
clean:Bool = false) throws -> Self
168168
{
169169
let checkout:SSGC.Checkout = try .checkout(project: projectName,
170170
from: repository,
171-
at: reference,
171+
at: refName,
172172
in: workspace,
173173
clean: clean)
174174

@@ -178,13 +178,13 @@ extension SSGC.PackageBuild
178178
let scratch:SSGC.PackageBuildDirectory = .init(configuration: .debug,
179179
location: checkout.location / scratchName)
180180

181-
let version:AnyVersion = .init(reference)
181+
let version:AnyVersion = .init(refName)
182182
let pin:SPM.DependencyPin = .init(identity: projectName,
183183
location: .remote(url: repository),
184184
revision: checkout.revision,
185185
version: version)
186186

187-
return .init(id: .versioned(pin, reference: reference),
187+
return .init(id: .versioned(pin, ref: refName, date: checkout.date),
188188
scratch: scratch,
189189
flags: flags,
190190
root: checkout.location,
@@ -227,7 +227,7 @@ extension SSGC.PackageBuild
227227
case .unversioned(let package):
228228
print("Discovering sources for book '\(package)' (unversioned)")
229229

230-
case .versioned(let pin, _):
230+
case .versioned(let pin, _, _):
231231
print("Discovering sources for book '\(pin.identity)' at \(pin.state)")
232232
}
233233

@@ -280,7 +280,7 @@ extension SSGC.PackageBuild
280280
case .unversioned(let package):
281281
print("Dumping manifest for package '\(package)' (unversioned)")
282282

283-
case .versioned(let pin, _):
283+
case .versioned(let pin, _, _):
284284
print("Dumping manifest for package '\(pin.identity)' at \(pin.state)")
285285
}
286286

Sources/SymbolGraphs/Metadata/SymbolGraphMetadata.Commit.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SHA1
2+
import UnixTime
23

34
extension SymbolGraphMetadata
45
{
@@ -11,21 +12,25 @@ extension SymbolGraphMetadata
1112
///
1213
/// It’s possible for multiple commits to have the same ref name. This is typical for
1314
/// branches.
14-
///
15-
/// It’s also possible for a commit to have multiple ref names. The name stored here
15+
///
16+
/// It’s also possible for a commit to have multiple ref names. The name stored here
1617
/// is whatever SwiftPM used to check out the sources.
1718
public
1819
var name:String
1920
/// The SHA-1 hash of the commit, nil if unknown. If the hash is unknown, the refname
2021
/// **must** point to a permanent tag.
2122
public
2223
var sha1:SHA1?
24+
/// The date of the commit, if known.
25+
public
26+
var date:UnixMillisecond?
2327

2428
@inlinable public
25-
init(name:String, sha1:SHA1? = nil)
29+
init(name:String, sha1:SHA1? = nil, date:UnixMillisecond? = nil)
2630
{
2731
self.name = name
2832
self.sha1 = sha1
33+
self.date = date
2934
}
3035
}
3136
}

Sources/SymbolGraphs/Metadata/SymbolGraphMetadata.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extension SymbolGraphMetadata
124124
case package_scope = "scope"
125125
case commit_hash = "revision"
126126
case commit_refname = "refname"
127+
case commit_date
127128
case triple
128129
case swift_version = "toolchain"
129130
case swift_nightly = "toolchain_type"
@@ -156,6 +157,7 @@ extension SymbolGraphMetadata:BSONDocumentEncodable
156157
bson[.package_scope] = self.package.scope
157158
bson[.commit_hash] = self.commit?.sha1
158159
bson[.commit_refname] = self.commit?.name
160+
bson[.commit_date] = self.commit?.date
159161
bson[.triple] = self.triple
160162
bson[.swift_version] = self.swift.version
161163
bson[.swift_nightly] = self.swift.nightly
@@ -180,7 +182,9 @@ extension SymbolGraphMetadata:BSONDocumentDecodable
180182
name: try bson[.package_name].decode()),
181183
commit: try bson[.commit_refname]?.decode(as: String.self)
182184
{
183-
.init(name: $0, sha1: try bson[.commit_hash]?.decode())
185+
.init(name: $0,
186+
sha1: try bson[.commit_hash]?.decode(),
187+
date: try bson[.commit_date]?.decode())
184188
},
185189
triple: try bson[.triple].decode(),
186190
swift: .init(
@@ -197,4 +201,3 @@ extension SymbolGraphMetadata:BSONDocumentDecodable
197201
self.abi = try bson[.abi].decode()
198202
}
199203
}
200-

Sources/SymbolGraphs/SymbolGraphABI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import SemanticVersions
33
@frozen public
44
enum SymbolGraphABI
55
{
6-
@inlinable public static var version:PatchVersion { .v(0, 12, 3) }
6+
@inlinable public static var version:PatchVersion { .v(0, 12, 4) }
77
}

0 commit comments

Comments
 (0)