|
1 | | -function readVersion($gitPath) { |
2 | | - $gitExecutable = "${gitPath}\git.exe" |
3 | | - |
4 | | - if (-not (Test-Path "$gitExecutable")) { |
5 | | - return $null |
6 | | - } |
7 | | - |
8 | | - $gitVersion = (cmd /c "${gitExecutable}" --version) |
9 | | - |
10 | | - if ($gitVersion -match 'git version') { |
11 | | - ($trash1, $trash2, $gitVersion) = $gitVersion.split(' ', 3) |
12 | | - } else { |
13 | | - pause |
14 | | - return $null |
15 | | - } |
16 | | - |
17 | | - return $gitVersion.toString() |
18 | | -} |
19 | | - |
20 | | -function isGitShim($gitPath) { |
21 | | - # check if there is a shim file - if yes, read the actual executable path |
22 | | - # See: github.com/ScoopInstaller/Shim |
23 | | - |
24 | | - if (Test-Path "${gitPath}\git.shim") { |
25 | | - $shim = (get-content "${gitPath}\git.shim") |
26 | | - ($trash, $gitPath) = $shim.replace(' ', '').split('=') |
27 | | - |
28 | | - $gitPath = $gitPath.replace('\git.exe', '') |
29 | | - } |
30 | | - |
31 | | - return $gitPath.toString() |
32 | | -} |
33 | | - |
34 | | -function compareVersions($userVersion, $vendorVersion) { |
35 | | - if ($null -ne $userVersion) { |
36 | | - ($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4) |
37 | | - } else { |
38 | | - return -1 |
39 | | - } |
40 | | - |
41 | | - if ($null -ne $vendorVersion) { |
42 | | - ($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4) |
43 | | - } else { |
44 | | - return 1 |
45 | | - } |
46 | | - |
47 | | - if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) { |
48 | | - return 1 |
49 | | - } |
50 | | - |
51 | | - if ($userMajor -gt $vendorMajor) { return 1 } |
52 | | - if ($userMajor -lt $vendorMajor) { return -1 } |
53 | | - |
54 | | - if ($userMinor -gt $vendorMinor) { return 1 } |
55 | | - if ($userMinor -lt $vendorMinor) { return -1 } |
56 | | - |
57 | | - if ($userPatch -gt $vendorPatch) { return 1 } |
58 | | - if ($userPatch -lt $vendorPatch) { return -1 } |
59 | | - |
60 | | - if ($userBuild -gt $vendorBuild) { return 1 } |
61 | | - if ($userBuild -lt $vendorBuild) { return -1 } |
62 | | - |
63 | | - return 0 |
64 | | -} |
65 | | - |
66 | | -function compare_git_versions($userVersion, $vendorVersion) { |
67 | | - $result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion |
68 | | - |
69 | | - Write-Debug "Compare Versions Result: ${result}" |
70 | | - if ($result -ge 0) { |
71 | | - return $userVersion |
72 | | - } |
73 | | - else { |
74 | | - return $vendorVersion |
75 | | - } |
76 | | -} |
77 | | - |
78 | | -function Configure-Git($gitRoot, $gitType, $gitPathUser) { |
79 | | - # Proposed Behavior |
80 | | - |
81 | | - # Modify the path if we are using VENDORED Git, do nothing if using USER Git. |
82 | | - # If User Git is installed but is older, match its path config adding paths |
83 | | - # in the same path positions allowing a user to configure Cmder Git path |
84 | | - # using locally installed Git Path Config. |
85 | | - if ($gitType -eq 'VENDOR') { |
86 | | - # If User Git is installed replace its path config with Newer Vendored Git Path |
87 | | - if (($null -ne $gitPathUser) -and ($gitPathUser -ne '')) { |
88 | | - Write-Verbose "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..." |
89 | | - |
90 | | - $newPath = ($env:path -ireplace [regex]::Escape($gitPathUser), $gitRoot) |
91 | | - } |
92 | | - else { |
93 | | - if (-not ($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { |
94 | | - Write-Debug "Adding $gitRoot\cmd to the path" |
95 | | - $newPath = $($gitRoot + "\cmd" + ";" + $env:Path) |
96 | | - } |
97 | | - |
98 | | - # Add "$gitRoot\mingw[32|64]\bin" to the path if exists and not done already |
99 | | - if ((Test-Path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) { |
100 | | - Write-Debug "Adding $gitRoot\mingw32\bin to the path" |
101 | | - $newPath = "$newPath;$gitRoot\mingw32\bin" |
102 | | - } |
103 | | - elseif ((Test-Path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) { |
104 | | - Write-Debug "Adding $gitRoot\mingw64\bin to the path" |
105 | | - $newPath = "$newPath;$gitRoot\mingw64\bin" |
106 | | - } |
107 | | - |
108 | | - # Add "$gitRoot\usr\bin" to the path if exists and not done already |
109 | | - if ((Test-Path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) { |
110 | | - Write-Debug "Adding $gitRoot\usr\bin to the path" |
111 | | - $newPath = "$newPath;$gitRoot\usr\bin" |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - return $newPath |
116 | | - } |
117 | | - |
118 | | - return $env:path |
119 | | -} |
120 | | - |
121 | | -function Import-Git() { |
122 | | - $GitModule = Get-Module -Name Posh-Git -ListAvailable |
123 | | - if ($GitModule | Select-Object version | Where-Object version -le ([version]"0.6.1.20160330")) { |
124 | | - Import-Module Posh-Git > $null |
125 | | - } |
126 | | - if ($GitModule | Select-Object version | Where-Object version -ge ([version]"1.0.0")) { |
127 | | - Import-Module Posh-Git > $null |
128 | | - $GitPromptSettings.AnsiConsole = $false |
129 | | - } |
130 | | - if (-not $GitModule) { |
131 | | - Write-Host -NoNewline "`r`n" |
132 | | - Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart Cmder." |
133 | | - Write-Host -NoNewline "`r$([char]0x1B)[A" |
134 | | - return $false |
135 | | - } |
136 | | - # Make sure we only run once by always returning true |
137 | | - return $true |
138 | | -} |
139 | | - |
140 | | -function checkGit($Path) { |
141 | | - if (-not (Get-Command git -ErrorAction SilentlyContinue)) { |
142 | | - return |
143 | | - } |
144 | | - if (-not (Test-Path -Path (Join-Path $Path '.git'))) { |
145 | | - $SplitPath = Split-Path $path |
146 | | - if ($SplitPath) { checkGit($SplitPath) } |
147 | | - return |
148 | | - } |
149 | | - if (getGitStatusSetting -eq $true) { |
150 | | - if ($null -eq $env:gitLoaded) { |
151 | | - $env:gitLoaded = Import-Git |
152 | | - } |
153 | | - if ($env:gitLoaded -eq $true) { |
154 | | - Write-VcsStatus |
155 | | - } |
156 | | - } |
157 | | - else { |
158 | | - $headContent = Get-Content (Join-Path $Path '.git/HEAD') |
159 | | - if ($headContent -like "ref: refs/heads/*") { |
160 | | - $branchName = $headContent.Substring(16) |
161 | | - } |
162 | | - else { |
163 | | - $branchName = "HEAD detached at $($headContent.Substring(0, 7))" |
164 | | - } |
165 | | - Write-Host " [$branchName]" -NoNewline -ForegroundColor White |
166 | | - } |
167 | | -} |
168 | | - |
169 | | -function getGitStatusSetting() { |
170 | | - $gitStatus = (git --no-pager config -l) | Out-String |
171 | | - |
172 | | - foreach ($line in $($gitStatus -split "`r`n")) { |
173 | | - if (($line -match 'cmder.status=false') -or ($line -match 'cmder.psstatus=false')) { |
174 | | - return $false |
175 | | - } |
176 | | - } |
177 | | - |
178 | | - return $true |
179 | | -} |
| 1 | +function readVersion($gitPath) { |
| 2 | + $gitExecutable = "${gitPath}\git.exe" |
| 3 | + |
| 4 | + if (-not (Test-Path "$gitExecutable")) { |
| 5 | + return $null |
| 6 | + } |
| 7 | + |
| 8 | + $gitVersion = (cmd /c "${gitExecutable}" --version) |
| 9 | + |
| 10 | + if ($gitVersion -match 'git version') { |
| 11 | + ($trash1, $trash2, $gitVersion) = $gitVersion.split(' ', 3) |
| 12 | + } else { |
| 13 | + pause |
| 14 | + return $null |
| 15 | + } |
| 16 | + |
| 17 | + return $gitVersion.toString() |
| 18 | +} |
| 19 | + |
| 20 | +function isGitShim($gitPath) { |
| 21 | + # check if there is a shim file - if yes, read the actual executable path |
| 22 | + # See: github.com/ScoopInstaller/Shim |
| 23 | + |
| 24 | + if (Test-Path "${gitPath}\git.shim") { |
| 25 | + $shim = (get-content "${gitPath}\git.shim") |
| 26 | + ($trash, $gitPath) = $shim.replace(' ', '').split('=') |
| 27 | + |
| 28 | + $gitPath = $gitPath.replace('\git.exe', '') |
| 29 | + } |
| 30 | + |
| 31 | + return $gitPath.toString() |
| 32 | +} |
| 33 | + |
| 34 | +function compareVersions($userVersion, $vendorVersion) { |
| 35 | + if ($null -ne $userVersion) { |
| 36 | + ($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4) |
| 37 | + } else { |
| 38 | + return -1 |
| 39 | + } |
| 40 | + |
| 41 | + if ($null -ne $vendorVersion) { |
| 42 | + ($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4) |
| 43 | + } else { |
| 44 | + return 1 |
| 45 | + } |
| 46 | + |
| 47 | + if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) { |
| 48 | + return 1 |
| 49 | + } |
| 50 | + |
| 51 | + if ($userMajor -gt $vendorMajor) { return 1 } |
| 52 | + if ($userMajor -lt $vendorMajor) { return -1 } |
| 53 | + |
| 54 | + if ($userMinor -gt $vendorMinor) { return 1 } |
| 55 | + if ($userMinor -lt $vendorMinor) { return -1 } |
| 56 | + |
| 57 | + if ($userPatch -gt $vendorPatch) { return 1 } |
| 58 | + if ($userPatch -lt $vendorPatch) { return -1 } |
| 59 | + |
| 60 | + if ($userBuild -gt $vendorBuild) { return 1 } |
| 61 | + if ($userBuild -lt $vendorBuild) { return -1 } |
| 62 | + |
| 63 | + return 0 |
| 64 | +} |
| 65 | + |
| 66 | +function compare_git_versions($userVersion, $vendorVersion) { |
| 67 | + $result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion |
| 68 | + |
| 69 | + Write-Debug "Compare Versions Result: ${result}" |
| 70 | + if ($result -ge 0) { |
| 71 | + return $userVersion |
| 72 | + } |
| 73 | + else { |
| 74 | + return $vendorVersion |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function Configure-Git($gitRoot, $gitType, $gitPathUser) { |
| 79 | + # Proposed Behavior |
| 80 | + |
| 81 | + # Modify the path if we are using VENDORED Git, do nothing if using USER Git. |
| 82 | + # If User Git is installed but is older, match its path config adding paths |
| 83 | + # in the same path positions allowing a user to configure Cmder Git path |
| 84 | + # using locally installed Git Path Config. |
| 85 | + if ($gitType -eq 'VENDOR') { |
| 86 | + # If User Git is installed replace its path config with Newer Vendored Git Path |
| 87 | + if (($null -ne $gitPathUser) -and ($gitPathUser -ne '')) { |
| 88 | + Write-Verbose "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..." |
| 89 | + |
| 90 | + $newPath = ($env:path -ireplace [regex]::Escape($gitPathUser), $gitRoot) |
| 91 | + } |
| 92 | + else { |
| 93 | + if (-not ($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { |
| 94 | + Write-Debug "Adding $gitRoot\cmd to the path" |
| 95 | + $newPath = $($gitRoot + "\cmd" + ";" + $env:Path) |
| 96 | + } |
| 97 | + |
| 98 | + # Add "$gitRoot\mingw[32|64]\bin" to the path if exists and not done already |
| 99 | + if ((Test-Path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) { |
| 100 | + Write-Debug "Adding $gitRoot\mingw32\bin to the path" |
| 101 | + $newPath = "$newPath;$gitRoot\mingw32\bin" |
| 102 | + } |
| 103 | + elseif ((Test-Path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) { |
| 104 | + Write-Debug "Adding $gitRoot\mingw64\bin to the path" |
| 105 | + $newPath = "$newPath;$gitRoot\mingw64\bin" |
| 106 | + } |
| 107 | + |
| 108 | + # Add "$gitRoot\usr\bin" to the path if exists and not done already |
| 109 | + if ((Test-Path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) { |
| 110 | + Write-Debug "Adding $gitRoot\usr\bin to the path" |
| 111 | + $newPath = "$newPath;$gitRoot\usr\bin" |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $newPath |
| 116 | + } |
| 117 | + |
| 118 | + return $env:path |
| 119 | +} |
| 120 | + |
| 121 | +function Import-Git() { |
| 122 | + $GitModule = Get-Module -Name Posh-Git -ListAvailable |
| 123 | + if ($GitModule | Select-Object version | Where-Object version -le ([version]"0.6.1.20160330")) { |
| 124 | + Import-Module Posh-Git > $null |
| 125 | + } |
| 126 | + if ($GitModule | Select-Object version | Where-Object version -ge ([version]"1.0.0")) { |
| 127 | + Import-Module Posh-Git > $null |
| 128 | + $GitPromptSettings.AnsiConsole = $false |
| 129 | + } |
| 130 | + if (-not $GitModule) { |
| 131 | + Write-Host -NoNewline "`r`n" |
| 132 | + Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart Cmder." |
| 133 | + Write-Host -NoNewline "`r$([char]0x1B)[A" |
| 134 | + return $false |
| 135 | + } |
| 136 | + # Make sure we only run once by always returning true |
| 137 | + return $true |
| 138 | +} |
| 139 | + |
| 140 | +function checkGit($Path) { |
| 141 | + if (-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 142 | + return |
| 143 | + } |
| 144 | + if (-not (Test-Path -Path (Join-Path $Path '.git'))) { |
| 145 | + $SplitPath = Split-Path $path |
| 146 | + if ($SplitPath) { checkGit($SplitPath) } |
| 147 | + return |
| 148 | + } |
| 149 | + if (getGitStatusSetting -eq $true) { |
| 150 | + if ($null -eq $env:gitLoaded) { |
| 151 | + $env:gitLoaded = Import-Git |
| 152 | + } |
| 153 | + if ($env:gitLoaded -eq $true) { |
| 154 | + Write-VcsStatus |
| 155 | + } |
| 156 | + } |
| 157 | + else { |
| 158 | + $headContent = Get-Content (Join-Path $Path '.git/HEAD') |
| 159 | + if ($headContent -like "ref: refs/heads/*") { |
| 160 | + $branchName = $headContent.Substring(16) |
| 161 | + } |
| 162 | + else { |
| 163 | + $branchName = "HEAD detached at $($headContent.Substring(0, 7))" |
| 164 | + } |
| 165 | + Write-Host " [$branchName]" -NoNewline -ForegroundColor White |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +function getGitStatusSetting() { |
| 170 | + $gitStatus = (git --no-pager config -l) | Out-String |
| 171 | + |
| 172 | + foreach ($line in $($gitStatus -split "`r`n")) { |
| 173 | + if (($line -match 'cmder.status=false') -or ($line -match 'cmder.psstatus=false')) { |
| 174 | + return $false |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return $true |
| 179 | +} |
0 commit comments