Skip to content

Commit 86c3018

Browse files
committed
[Utility] Add support for version-specific repository tags.
- This is the second part of SE-0135.
1 parent 04a1505 commit 86c3018

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

Sources/TestSupport/misc.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public func fixture(name: String, tags: [String] = [], file: StaticString = #fil
8484

8585
/// Test-helper function that creates a new Git repository in a directory. The new repository will contain exactly one empty file, and if a tag name is provided, a tag with that name will be created.
8686
public func initGitRepo(_ dir: AbsolutePath, tag: String? = nil, file: StaticString = #file, line: UInt = #line) {
87+
initGitRepo(dir, tags: tag.flatMap{ [$0] } ?? [], file: file, line: line)
88+
}
89+
90+
public func initGitRepo(_ dir: AbsolutePath, tags: [String], file: StaticString = #file, line: UInt = #line) {
8791
do {
8892
let file = dir.appending(component: "file.swift")
8993
try systemQuietly(["touch", file.asString])
@@ -92,7 +96,7 @@ public func initGitRepo(_ dir: AbsolutePath, tag: String? = nil, file: StaticStr
9296
try systemQuietly([Git.tool, "-C", dir.asString, "config", "user.name", "Example Example"])
9397
try systemQuietly([Git.tool, "-C", dir.asString, "add", "."])
9498
try systemQuietly([Git.tool, "-C", dir.asString, "commit", "-m", "msg"])
95-
if let tag = tag {
99+
for tag in tags {
96100
try tagGitRepo(dir, tag: tag)
97101
}
98102
}

Sources/Utility/Git.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,34 @@ public class Git {
5959
let out = (try? Git.runPopen([Git.tool, "-C", repo.path.asString, "tag", "-l"])) ?? ""
6060
let tags = out.characters.split(separator: "\n").map{ String($0) }
6161

62-
// First try the plain init.
62+
// First, check if we need to restrict the tag set to version-specific tags.
6363
var knownVersions: [Version: String] = [:]
64+
for versionSpecificKey in Versioning.currentVersionSpecificKeys {
65+
for tag in tags {
66+
if tag.hasSuffix(versionSpecificKey) {
67+
let specifier = String(tag.characters.dropLast(versionSpecificKey.characters.count))
68+
if let version = Version(specifier) ?? Version.vprefix(specifier) {
69+
knownVersions[version] = tag
70+
}
71+
}
72+
}
73+
74+
// If we found tags at this version-specific key, we are done.
75+
if !knownVersions.isEmpty {
76+
return knownVersions
77+
}
78+
}
79+
80+
// Otherwise, look for normal tags.
6481
for tag in tags {
6582
if let version = Version(tag) {
6683
knownVersions[version] = tag
6784
}
6885
}
86+
6987
// If we didn't find any versions, look for 'v'-prefixed ones.
88+
//
89+
// FIXME: We should match both styles simultaneously.
7090
if knownVersions.isEmpty {
7191
for tag in tags {
7292
if let version = Version.vprefix(tag) {

Tests/GetTests/GitTests.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
import XCTest
1212

13-
import TestSupport
1413
import Basic
14+
import Utility
15+
16+
import TestSupport
1517

1618
import class PackageDescription.Package
17-
import class Utility.Git
1819
import struct PackageDescription.Version
1920
import struct PackageModel.Manifest
2021

@@ -34,6 +35,30 @@ class GitTests: XCTestCase {
3435
}
3536
}
3637

38+
func testVersionSpecificTags() {
39+
let current = Versioning.currentVersion
40+
mktmpdir { path in
41+
let gitRepo = makeGitRepo(path, tags: ["0.1.0", "0.2.0@swift-\(current.major)", "0.3.0@swift-\(current.major).\(current.minor)", "0.4.0@swift-\(current.major).\(current.minor).\(current.patch)"])!
42+
XCTAssertTrue(gitRepo.hasVersion)
43+
XCTAssertEqual(gitRepo.versions, [Version(0,4,0)])
44+
}
45+
mktmpdir { path in
46+
let gitRepo = makeGitRepo(path, tags: ["0.1.0", "0.2.0@swift-\(current.major)", "0.3.0@swift-\(current.major).\(current.minor)"])!
47+
XCTAssertTrue(gitRepo.hasVersion)
48+
XCTAssertEqual(gitRepo.versions, [Version(0,3,0)])
49+
}
50+
mktmpdir { path in
51+
let gitRepo = makeGitRepo(path, tags: ["0.1.0", "0.2.0@swift-\(current.major)"])!
52+
XCTAssertTrue(gitRepo.hasVersion)
53+
XCTAssertEqual(gitRepo.versions, [Version(0,2,0)])
54+
}
55+
mktmpdir { path in
56+
let gitRepo = makeGitRepo(path, tags: ["0.1.0", "v0.2.0@swift-\(current.major)"])!
57+
XCTAssertTrue(gitRepo.hasVersion)
58+
XCTAssertEqual(gitRepo.versions, [Version(0,2,0)])
59+
}
60+
}
61+
3762
func testHasNoVersion() {
3863
mktmpdir { path in
3964
let gitRepo = makeGitRepo(path, tag: nil)!
@@ -51,6 +76,7 @@ class GitTests: XCTestCase {
5176

5277
static var allTests = [
5378
("testHasVersion", testHasVersion),
79+
("testVersionSpecificTags", testVersionSpecificTags),
5480
("testHasNoVersion", testHasNoVersion),
5581
("testCloneShouldNotCrashWihoutTags", testCloneShouldNotCrashWihoutTags),
5682
("testCloneShouldCrashWihoutTags", testCloneShouldCrashWihoutTags),
@@ -60,7 +86,11 @@ class GitTests: XCTestCase {
6086
//MARK: - Helpers
6187

6288
func makeGitRepo(_ dstdir: AbsolutePath, tag: String? = nil, file: StaticString = #file, line: UInt = #line) -> Git.Repo? {
63-
initGitRepo(dstdir, tag: tag)
89+
return makeGitRepo(dstdir, tags: tag.flatMap{ [$0] } ?? [], file: file, line: line)
90+
}
91+
92+
func makeGitRepo(_ dstdir: AbsolutePath, tags: [String], file: StaticString = #file, line: UInt = #line) -> Git.Repo? {
93+
initGitRepo(dstdir, tags: tags)
6494
return Git.Repo(path: dstdir)
6595
}
6696

0 commit comments

Comments
 (0)