@@ -5,32 +5,44 @@ function New-InitiateGitRepo {
55 [string ]$DirectoryPath
66 )
77
8- # Check if Git is installed
9- if (-not (Get-Command git - ErrorAction SilentlyContinue)) {
10- Write-Warning ' Git is not installed. Please install Git and initialize repo manually'
8+ if (-not (Test-NovaGitCommandAvailable )) {
9+ Write-Warning ' Git is not installed. Please install Git and initialize repo manually'
1110 return
1211 }
13- Push-Location - StackName ' GitInit'
12+
13+ if (Test-Path - LiteralPath (Join-Path $DirectoryPath ' .git' )) {
14+ Write-Warning ' A Git repository already exists in this directory.'
15+ return
16+ }
17+
18+ if (-not $PSCmdlet.ShouldProcess ($DirectoryPath , " Initiating git on $DirectoryPath " )) {
19+ return
20+ }
21+
1422 try {
15- # Navigate to the specified directory
16- Set-Location $DirectoryPath
17-
18- # Check if a Git repository already exists
19- if (Test-Path - Path ' .git' ) {
20- Write-Warning ' A Git repository already exists in this directory.'
21- return
22- }
23-
24- if ( $PSCmdlet.ShouldProcess ($DirectoryPath , (" Initiating git on $DirectoryPath " ))) {
25- try {
26- git init | Out-Null
27- } catch {
28- Stop-NovaOperation - Message " Failed to initialize Git repo: $ ( $_.Exception.Message ) " - ErrorId ' Nova.Dependency.GitRepositoryInitializationFailed' - Category OpenError - TargetObject $DirectoryPath
29- }
30- }
31- Write-Verbose ' Git repository initialized successfully'
23+ $result = Invoke-NovaGitCommand - ProjectRoot $DirectoryPath - Arguments @ (' init' )
3224 }
33- finally {
34- Pop-Location - StackName ' GitInit'
25+ catch {
26+ Stop-NovaOperation - Message " Failed to initialize Git repo: $ ( $_.Exception.Message ) " - ErrorId ' Nova.Dependency.GitRepositoryInitializationFailed' - Category OpenError - TargetObject $DirectoryPath
27+ }
28+
29+ if ($result.ExitCode -ne 0 ) {
30+ Stop-NovaOperation - Message (Get-NovaGitInitializationFailureMessage - Result $result ) - ErrorId ' Nova.Dependency.GitRepositoryInitializationFailed' - Category OpenError - TargetObject $DirectoryPath
3531 }
32+
33+ Write-Verbose ' Git repository initialized successfully'
34+ }
35+
36+ function Get-NovaGitInitializationFailureMessage {
37+ [CmdletBinding ()]
38+ param (
39+ [Parameter (Mandatory )][pscustomobject ]$Result
40+ )
41+
42+ $details = Get-NovaGitCommandOutputText - Result $Result
43+ if ( [string ]::IsNullOrWhiteSpace($details )) {
44+ return ' Failed to initialize Git repo.'
45+ }
46+
47+ return " Failed to initialize Git repo: $details "
3648}
0 commit comments