11
11
import class Foundation. ProcessInfo
12
12
import TSCBasic
13
13
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
-
26
14
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
-
59
15
/// A shell command to run for Git. Can be either a name or a path.
60
16
/// - Note: modification is not thread safe, designed for testing only
61
17
public static var tool : String = " git \( executableFileSuffix) "
@@ -70,20 +26,55 @@ public enum Git {
70
26
}
71
27
}
72
28
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
+ }
74
65
75
66
/// Returns the environment variables for launching the git subprocess.
76
67
///
77
68
/// This contains the current environment with custom overrides for using
78
69
/// git from swift build.
79
70
/// - Note: modification is not thread safe, designed for testing only
80
- public static var environment : [ String : String ] {
71
+ public static var environmentBlock : ProcessEnvironmentBlock {
81
72
get {
82
73
var env = Self . _gitEnvironment
83
74
84
75
// These variables are inserted into the environment when shelling out
85
76
// to git if not already present.
86
- let underrideVariables = [
77
+ let underrideVariables : ProcessEnvironmentBlock = [
87
78
// Disable terminal prompts in git. This will make git error out and return
88
79
// when it needs a user/pass etc instead of hanging the terminal (SR-3981).
89
80
" GIT_TERMINAL_PROMPT " : " 0 " ,
0 commit comments