Skip to content

Commit b503faf

Browse files
🩹 [Patch]: Improve error handling in Git configuration scripts (#257)
## Description This pull request includes changes to improve error handling in the `Get-GitHubGitConfig.ps1` and `Set-GitHubGitConfig.ps1` scripts. The most important changes include replacing exceptions with warnings and adding checks to ensure the commands are run inside a Git repository. Error handling improvements: * [`src/functions/public/Git/Get-GitHubGitConfig.ps1`](diffhunk://#diff-dfb306c31ba449aae53bfc9d39801cb64924641ebf9f953e6eed74a02877ee40L28-R34): Replaced the exception thrown when Git is not installed with a warning message and added a check to ensure the command is run inside a Git repository. * [`src/functions/public/Git/Set-GitHubGitConfig.ps1`](diffhunk://#diff-9b7705b192d0886bb8d7c2ee853a341fee0be08850a23e15955110e7105c803bL38-R44): Replaced the exception thrown when Git is not installed with a warning message and added a check to ensure the command is run inside a Git repository. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 6ca1d3b commit b503faf

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/functions/public/Git/Get-GitHubGitConfig.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@
2424
try {
2525

2626
$gitExists = Get-Command -Name 'git' -ErrorAction SilentlyContinue
27+
Write-Debug "GITEXISTS: $gitExists"
2728
if (-not $gitExists) {
28-
throw 'Git is not installed. Please install Git before running this command.'
29+
Write-Verbose "Git is not installed. Cannot get git configuration."
30+
return
31+
}
32+
33+
$cmdresult = git rev-parse --is-inside-work-tree 2>&1
34+
Write-Debug "LASTEXITCODE: $LASTEXITCODE"
35+
Write-Debug "CMDRESULT: $cmdresult"
36+
if ($LASTEXITCODE -ne 0) {
37+
Write-Verbose 'Not a git repository. Cannot get git configuration.'
38+
$Global:LASTEXITCODE = 0
39+
Write-Debug "Resetting LASTEXITCODE: $LASTEXITCODE"
40+
return
2941
}
3042

3143
git config --local --list | ForEach-Object {

src/functions/public/Git/Set-GitHubGitConfig.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,20 @@
3434
process {
3535
try {
3636
$gitExists = Get-Command -Name 'git' -ErrorAction SilentlyContinue
37+
Write-Debug "GITEXISTS: $gitExists"
3738
if (-not $gitExists) {
38-
throw 'Git is not installed. Please install Git before running this command.'
39+
Write-Verbose 'Git is not installed. Cannot configure git.'
40+
return
41+
}
42+
43+
$cmdresult = git rev-parse --is-inside-work-tree 2>&1
44+
Write-Debug "LASTEXITCODE: $LASTEXITCODE"
45+
Write-Debug "CMDRESULT: $cmdresult"
46+
if ($LASTEXITCODE -ne 0) {
47+
Write-Verbose 'Not a git repository. Cannot configure git.'
48+
$Global:LASTEXITCODE = 0
49+
Write-Debug "Resetting LASTEXITCODE: $LASTEXITCODE"
50+
return
3951
}
4052

4153
$username = $Context.UserName

0 commit comments

Comments
 (0)