Skip to content

Commit 930e82e

Browse files
authored
Update Git type for ProcessEnvironmentBlock (#470)
Previous implementation did not account for case insensitivity on Windows and made it hard to remove deprecation warnings in clients of TSC.
1 parent ae1b5ee commit 930e82e

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

Sources/TSCUtility/Git.swift

+38-47
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,7 @@
1111
import class Foundation.ProcessInfo
1212
import TSCBasic
1313

14-
extension Version {
15-
// FIXME: deprecate 2/2021 (used below), remove once clients transitioned
16-
@available(*, deprecated, message: "moved to SwiftPM")
17-
init?(tag: String) {
18-
if tag.first == "v" {
19-
self.init(string: String(tag.dropFirst()))
20-
} else {
21-
self.init(string: tag)
22-
}
23-
}
24-
}
25-
2614
public enum Git {
27-
// FIXME: deprecate 2/2021, remove once clients transitioned
28-
@available(*, deprecated, message: "moved to SwiftPM")
29-
public static func convertTagsToVersionMap(_ tags: [String]) -> [Version: [String]] {
30-
// First, check if we need to restrict the tag set to version-specific tags.
31-
var knownVersions: [Version: [String]] = [:]
32-
var versionSpecificKnownVersions: [Version: [String]] = [:]
33-
34-
for tag in tags {
35-
for versionSpecificKey in Versioning.currentVersionSpecificKeys {
36-
if tag.hasSuffix(versionSpecificKey) {
37-
let trimmedTag = String(tag.dropLast(versionSpecificKey.count))
38-
if let version = Version(tag: trimmedTag) {
39-
versionSpecificKnownVersions[version, default: []].append(tag)
40-
}
41-
break
42-
}
43-
}
44-
45-
if let version = Version(tag: tag) {
46-
knownVersions[version, default: []].append(tag)
47-
}
48-
}
49-
// Check if any version specific tags were found.
50-
// If true, then return the version specific tags,
51-
// or else return the version independent tags.
52-
if !versionSpecificKnownVersions.isEmpty {
53-
return versionSpecificKnownVersions
54-
} else {
55-
return knownVersions
56-
}
57-
}
58-
5915
/// A shell command to run for Git. Can be either a name or a path.
6016
/// - Note: modification is not thread safe, designed for testing only
6117
public static var tool: String = "git\(executableFileSuffix)"
@@ -70,20 +26,55 @@ public enum Git {
7026
}
7127
}
7228

73-
private static var _gitEnvironment = ProcessInfo.processInfo.environment
29+
private static var _gitEnvironment = ProcessEnv.block
30+
31+
@available(*,
32+
deprecated,
33+
renamed: "environmentBlock",
34+
message: "Previous `[String: String]` representation did not account for case insensitivity on Windows."
35+
)
36+
public static var environment: [String: String] {
37+
get {
38+
var env = Self._gitEnvironment
39+
40+
// These variables are inserted into the environment when shelling out
41+
// to git if not already present.
42+
let underrideVariables: ProcessEnvironmentBlock = [
43+
// Disable terminal prompts in git. This will make git error out and return
44+
// when it needs a user/pass etc instead of hanging the terminal (SR-3981).
45+
"GIT_TERMINAL_PROMPT": "0",
46+
47+
// The above is env variable is not enough. However, ssh_config's batch
48+
// mode is made for this purpose. see: https://linux.die.net/man/5/ssh_config
49+
"GIT_SSH_COMMAND": "ssh -oBatchMode=yes",
50+
]
51+
52+
for (key, value) in underrideVariables {
53+
// Skip this key is already present in the env.
54+
if env.keys.contains(key) { continue }
55+
56+
env[key] = value
57+
}
58+
59+
return Dictionary(env.map { ($0.value, $1) }, uniquingKeysWith: { $1 })
60+
}
61+
set {
62+
Self._gitEnvironment = .init(newValue)
63+
}
64+
}
7465

7566
/// Returns the environment variables for launching the git subprocess.
7667
///
7768
/// This contains the current environment with custom overrides for using
7869
/// git from swift build.
7970
/// - Note: modification is not thread safe, designed for testing only
80-
public static var environment: [String: String] {
71+
public static var environmentBlock: ProcessEnvironmentBlock {
8172
get {
8273
var env = Self._gitEnvironment
8374

8475
// These variables are inserted into the environment when shelling out
8576
// to git if not already present.
86-
let underrideVariables = [
77+
let underrideVariables: ProcessEnvironmentBlock = [
8778
// Disable terminal prompts in git. This will make git error out and return
8879
// when it needs a user/pass etc instead of hanging the terminal (SR-3981).
8980
"GIT_TERMINAL_PROMPT": "0",

0 commit comments

Comments
 (0)