@@ -50,31 +50,84 @@ function Get-StdGitPath {
5050 return $null
5151}
5252
53- function Add-ToUserPath {
53+ # Ensure $PathToAdd is inserted before any PATH entry that contains "git" (case-insensitive)
54+ # Updates Machine (system) PATH; if not elevated, emits a prominent error with instructions
55+ function Set-PathPrependBeforeGit {
5456 param (
5557 [Parameter (Mandatory = $true )][string ]$PathToAdd
5658 )
57- $current = [ Environment ]::GetEnvironmentVariable( ' Path ' , ' User ' )
59+
5860 $sep = ' ;'
59- $entries = @ ()
60- if ($current ) { $entries = ($current -split $sep ) | Where-Object { $_ -and $_.Trim () -ne ' ' } }
61-
62- $exists = $false
63- foreach ($entry in $entries ) {
64- try {
65- if ([IO.Path ]::GetFullPath($entry.TrimEnd (' \' )) -ieq [IO.Path ]::GetFullPath($PathToAdd.TrimEnd (' \' ))) {
66- $exists = $true
67- break
61+
62+ function NormalizePath ([string ]$p ) {
63+ try { return ([IO.Path ]::GetFullPath($p.Trim ())).TrimEnd(' \\' ).ToLowerInvariant() }
64+ catch { return ($p.Trim ()).TrimEnd(' \\' ).ToLowerInvariant() }
65+ }
66+
67+ $normalizedAdd = NormalizePath $PathToAdd
68+
69+ # Helper to build new PATH string with PathToAdd inserted before first 'git' entry
70+ function BuildPathWithInsert ([string ]$existingPath , [string ]$toInsert ) {
71+ $entries = @ ()
72+ if ($existingPath ) { $entries = ($existingPath -split $sep ) | Where-Object { $_ -and $_.Trim () -ne ' ' } }
73+
74+ # De-duplicate and remove any existing instance of $toInsert
75+ $list = New-Object System.Collections.Generic.List[string ]
76+ $seen = New-Object ' System.Collections.Generic.HashSet[string]'
77+ foreach ($e in $entries ) {
78+ $n = NormalizePath $e
79+ if (-not $seen.Contains ($n ) -and $n -ne $normalizedAdd ) {
80+ $seen.Add ($n ) | Out-Null
81+ $list.Add ($e ) | Out-Null
6882 }
69- } catch { }
83+ }
84+
85+ # Find first index that matches 'git' anywhere (case-insensitive)
86+ $insertIndex = 0
87+ for ($i = 0 ; $i -lt $list.Count ; $i ++ ) {
88+ if ($list [$i ] -match ' (?i)git' ) { $insertIndex = $i ; break }
89+ }
90+
91+ $list.Insert ($insertIndex , $toInsert )
92+ return ($list -join $sep )
7093 }
7194
72- if (-not $exists ) {
73- $newPath = if ($current ) { ($current.TrimEnd ($sep ) + $sep + $PathToAdd ) } else { $PathToAdd }
74- [Environment ]::SetEnvironmentVariable(' Path' , $newPath , ' User' )
75- return $true
95+ # Try to update Machine PATH
96+ $updatedScope = $null
97+ try {
98+ $machinePath = [Environment ]::GetEnvironmentVariable(' Path' , ' Machine' )
99+ $newMachinePath = BuildPathWithInsert - existingPath $machinePath - toInsert $PathToAdd
100+ if ($newMachinePath -ne $machinePath ) {
101+ [Environment ]::SetEnvironmentVariable(' Path' , $newMachinePath , ' Machine' )
102+ $updatedScope = ' Machine'
103+ } else {
104+ # Nothing changed at Machine scope; still treat as Machine for reporting
105+ $updatedScope = ' Machine'
106+ }
107+ } catch {
108+ # Access denied or not elevated; do NOT modify User PATH. Print big red error with instructions.
109+ $origGit = $null
110+ try { $origGit = Get-StdGitPath } catch { }
111+ $origGitDir = if ($origGit ) { (Split-Path $origGit - Parent) } else { ' your Git installation directory' }
112+ Write-Host ' '
113+ Write-Host ' ERROR: Unable to update the SYSTEM PATH (administrator rights required).' - ForegroundColor Red
114+ Write-Host ' Your PATH was NOT changed. To ensure git-ai takes precedence over Git:' - ForegroundColor Red
115+ Write-Host (" 1) Run PowerShell as Administrator and re-run this installer; OR" ) - ForegroundColor Red
116+ Write-Host (" 2) Manually edit the SYSTEM Path and move '{0}' before any entries containing 'Git' (e.g. '{1}')." -f $PathToAdd , $origGitDir ) - ForegroundColor Red
117+ Write-Host " Steps: Start → type 'Environment Variables' → 'Edit the system environment variables' → Environment Variables →" - ForegroundColor Red
118+ Write-Host " Under 'System variables', select 'Path' → Edit → Move '{0}' to the top (before Git) → OK." -f $PathToAdd - ForegroundColor Red
119+ Write-Host ' '
120+ $updatedScope = ' Error'
76121 }
77- return $false
122+
123+ # Update current process PATH immediately for this session
124+ try {
125+ $procPath = $env: PATH
126+ $newProcPath = BuildPathWithInsert - existingPath $procPath - toInsert $PathToAdd
127+ if ($newProcPath -ne $procPath ) { $env: PATH = $newProcPath }
128+ } catch { }
129+
130+ return $updatedScope
78131}
79132
80133# Detect architecture and OS
@@ -141,26 +194,22 @@ if ($stdGitPath) {
141194 $env: GIT_AI_GIT_PATH = $stdGitPath
142195}
143196
144- # TODO Install hooks
145- # Write-Host 'Setting up IDE/agent hooks...'
146- # try {
147- # & $finalExe install-hooks | Out-Host
148- # Write-Success 'Successfully set up IDE/agent hooks'
149- # } catch {
150- # Write-Host 'Warning: Failed to set up IDE/agent hooks; continuing without IDE/agent hooks.' -ForegroundColor Yellow
151- # }
152-
153- Write-Success " Successfully installed git-ai into $installDir "
154- Write-Success " You can now run 'git-ai' from your terminal"
155-
156- # Update PATH for the user if needed
157- $added = Add-ToUserPath - PathToAdd $installDir
158- if ($added ) {
159- if ($env: PATH -notmatch [Regex ]::Escape($installDir )) {
160- $env: PATH = " $installDir ;" + $env: PATH
161- }
162- Write-Success " Updated your user PATH to include $installDir "
163- Write-Host ' Restart your terminal for the change to take effect.'
197+ # Install hooks
198+ Write-Host ' Setting up IDE/agent hooks...'
199+ try {
200+ & $finalExe install-hooks | Out-Host
201+ Write-Success ' Successfully set up IDE/agent hooks'
202+ } catch {
203+ Write-Host ' Warning: Failed to set up IDE/agent hooks; continuing without IDE/agent hooks.' - ForegroundColor Yellow
164204}
165205
206+ # Update PATH so our shim takes precedence over any Git entries
207+ $scope = Set-PathPrependBeforeGit - PathToAdd $installDir
208+ if ($scope -eq ' Machine' ) {
209+ Write-Success ' Successfully added git-ai to the system PATH.'
210+ } elseif ($scope -eq ' Error' ) {
211+ Write-Host ' PATH update failed: system PATH unchanged.' - ForegroundColor Red
212+ }
166213
214+ Write-Success " Successfully installed git-ai into $installDir "
215+ Write-Success " You can now run 'git-ai' from your terminal"
0 commit comments