Skip to content

Commit 8e12654

Browse files
committed
[Utility] Sink version extraction into Utility.Git.
1 parent 8285334 commit 8e12654

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let package = Package(
4343
Target(
4444
/** Abstractions for common operations, should migrate to Basic */
4545
name: "Utility",
46-
dependencies: ["POSIX", "Basic"]),
46+
dependencies: ["POSIX", "Basic", "PackageDescription"]),
47+
// FIXME: We should be kill the PackageDescription dependency above.
4748
Target(
4849
/** Source control operations */
4950
name: "SourceControl",

Sources/Get/Git.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,3 @@ extension Git {
4646
return Repo(path: dstdir)! //TODO no bangs
4747
}
4848
}
49-
50-
extension Git.Repo {
51-
var versions: [Version] {
52-
let out = (try? Git.runPopen([Git.tool, "-C", path.asString, "tag", "-l"])) ?? ""
53-
let tags = out.characters.split(separator: "\n")
54-
let versions = tags.flatMap(Version.init).sorted()
55-
if !versions.isEmpty {
56-
return versions
57-
} else {
58-
return tags.flatMap(Version.vprefix).sorted()
59-
}
60-
}
61-
62-
/// Check if repo contains a version tag
63-
var hasVersion: Bool {
64-
return !versions.isEmpty
65-
}
66-
}

Sources/Get/Version.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@ extension Version {
2323
static var maxRange: Range<Version> {
2424
return self.min..<self.max
2525
}
26-
27-
static func vprefix(_ string: String.CharacterView) -> Version? {
28-
if string.first == "v" {
29-
return Version(string.dropFirst())
30-
} else {
31-
return nil
32-
}
33-
}
3426
}

Sources/Utility/Git.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ import func POSIX.getenv
1414
import libc
1515
import class Foundation.ProcessInfo
1616

17+
import struct PackageDescription.Version
18+
19+
extension Version {
20+
static func vprefix(_ string: String) -> Version? {
21+
if string.characters.first == "v" {
22+
return Version(string.characters.dropFirst())
23+
} else {
24+
return nil
25+
}
26+
}
27+
}
28+
1729
public class Git {
1830
public class Repo {
1931
public let path: AbsolutePath
@@ -41,6 +53,40 @@ public class Git {
4153
}
4254
}(self)
4355

56+
/// The set of known versions and their tags.
57+
public lazy var knownVersions: [Version: String] = { repo in
58+
// Get the list of tags.
59+
let out = (try? Git.runPopen([Git.tool, "-C", repo.path.asString, "tag", "-l"])) ?? ""
60+
let tags = out.characters.split(separator: "\n").map{ String($0) }
61+
62+
// First try the plain init.
63+
var knownVersions: [Version: String] = [:]
64+
for tag in tags {
65+
if let version = Version(tag) {
66+
knownVersions[version] = tag
67+
}
68+
}
69+
// If we didn't find any versions, look for 'v'-prefixed ones.
70+
if knownVersions.isEmpty {
71+
for tag in tags {
72+
if let version = Version.vprefix(tag) {
73+
knownVersions[version] = tag
74+
}
75+
}
76+
}
77+
return knownVersions
78+
}(self)
79+
80+
/// The set of versions in the repository.
81+
public var versions: [Version] {
82+
return [Version](knownVersions.keys)
83+
}
84+
85+
/// Check if repo contains a version tag
86+
public var hasVersion: Bool {
87+
return !versions.isEmpty
88+
}
89+
4490
public var branch: String! {
4591
return try? Git.runPopen([Git.tool, "-C", path.asString, "rev-parse", "--abbrev-ref", "HEAD"]).chomp()
4692
}

Tests/GetTests/GitTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@ import XCTest
1212

1313
import TestSupport
1414
import Basic
15-
@testable import Get
16-
import struct PackageModel.Manifest
15+
1716
import class PackageDescription.Package
1817
import class Utility.Git
18+
import struct PackageDescription.Version
19+
import struct PackageModel.Manifest
20+
21+
@testable import Get
1922

2023
class GitTests: XCTestCase {
2124
func testHasVersion() {
2225
mktmpdir { path in
2326
let gitRepo = makeGitRepo(path, tag: "0.1.0")!
2427
XCTAssertTrue(gitRepo.hasVersion)
28+
XCTAssertEqual(gitRepo.versions, [Version(0,1,0)])
29+
}
30+
mktmpdir { path in
31+
let gitRepo = makeGitRepo(path, tag: "v0.1.0")!
32+
XCTAssertTrue(gitRepo.hasVersion)
33+
XCTAssertEqual(gitRepo.versions, [Version(0,1,0)])
2534
}
2635
}
2736

0 commit comments

Comments
 (0)