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