Skip to content

Commit f86295e

Browse files
HUMORCEse35710
authored andcommitted
refactor(git): Use 'git -C' to specify the work directory instead of 'Push-Location'/'Pop-Location' (ScoopInstaller#4697)
* add `git_cmd` -without any preset command/args * remove unnecessary import - `git.ps1` * stop `Push-Location` and `Pop-Location` before/after git operations * stop `Push-Location` and `Pop-Location` before/after git operations * consistency of naming variable * CHANGELOG * punctuation mark * remove functions that are no longer used - `git_fetch` - `git_pull` - `get_checkout`
1 parent c4a83e8 commit f86295e

7 files changed

+45
-69
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- **mklink:** Use 'New-Item' instead of 'mklink' ([#4690](https://github.com/ScoopInstaller/Scoop/issues/4690))
3333
- **rmdir:** Use 'Remove-Item' instead of 'rmdir' ([#4691](https://github.com/ScoopInstaller/Scoop/issues/4691))
3434
- **COMSPEC:** Deprecate use of subshell cmd.exe ([#4692](https://github.com/ScoopInstaller/Scoop/pull/4692))
35+
- **git:** Use 'git -C' to specify the work directory instead of 'Push-Location'/'Pop-Location' ([#4697](https://github.com/ScoopInstaller/Scoop/pull/4697))
3536

3637
### Builds
3738

bin/scoop.ps1

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ reset_aliases
1212

1313
$commands = commands
1414
if ('--version' -contains $cmd -or (!$cmd -and '-v' -contains $args)) {
15-
Push-Location $(versiondir 'scoop' 'current')
16-
write-host "Current Scoop version:"
17-
Invoke-Expression "git --no-pager log --oneline HEAD -n 1"
18-
write-host ""
19-
Pop-Location
15+
Write-Host "Current Scoop version:"
16+
Invoke-Expression "git -C '$(versiondir 'scoop' 'current')' --no-pager log --oneline HEAD -n 1"
17+
Write-Host ""
2018

2119
Get-LocalBucket | ForEach-Object {
22-
Push-Location (Find-BucketDirectory $_ -Root)
23-
if(test-path '.git') {
24-
write-host "'$_' bucket:"
25-
Invoke-Expression "git --no-pager log --oneline HEAD -n 1"
26-
write-host ""
20+
$bucketLoc = Find-BucketDirectory $_ -Root
21+
if(Test-Path (Join-Path $bucketLoc '.git')) {
22+
Write-Host "'$_' bucket:"
23+
Invoke-Expression "git -C '$bucketLoc' --no-pager log --oneline HEAD -n 1"
24+
Write-Host ""
2725
}
28-
Pop-Location
2926
}
3027
}
3128
elseif (@($null, '--help', '/?') -contains $cmd -or $args[0] -contains '-h') { exec 'help' $args }

lib/buckets.ps1

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ function new_issue_msg($app, $bucket, $title, $body) {
127127
$bucket_path = "$bucketsdir\$bucket"
128128

129129
if (Test-path $bucket_path) {
130-
Push-Location $bucket_path
131-
$remote = Invoke-Expression "git config --get remote.origin.url"
130+
$remote = Invoke-Expression "git -C '$bucket_path' config --get remote.origin.url"
132131
# Support ssh and http syntax
133132
# git@PROVIDER:USER/REPO.git
134133
# https://PROVIDER/USER/REPO.git
135134
$remote -match '(@|:\/\/)(?<provider>.+)[:/](?<user>.*)\/(?<repo>.*)(\.git)?$' | Out-Null
136135
$url = "https://$($Matches.Provider)/$($Matches.User)/$($Matches.Repo)"
137-
Pop-Location
138136
}
139137

140138
if(!$url) { return 'Please contact the bucket maintainer!' }

lib/git.ps1

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function git_proxy_cmd {
22
$proxy = get_config 'proxy'
33
$cmd = "git $($args | ForEach-Object { "$_ " })"
4-
if($proxy -and $proxy -ne 'none') {
4+
if ($proxy -and $proxy -ne 'none') {
55
$cmd = "SET HTTPS_PROXY=$proxy&&SET HTTP_PROXY=$proxy&&$cmd"
66
}
77
cmd.exe /d /c $cmd
@@ -15,18 +15,6 @@ function git_ls_remote {
1515
git_proxy_cmd ls-remote $args
1616
}
1717

18-
function git_checkout {
19-
git_proxy_cmd checkout $args
20-
}
21-
22-
function git_pull {
23-
git_proxy_cmd pull --rebase=false $args
24-
}
25-
26-
function git_fetch {
27-
git_proxy_cmd fetch $args
28-
}
29-
30-
function git_checkout {
31-
git_proxy_cmd checkout $args
18+
function git_cmd {
19+
git_proxy_cmd $args
3220
}

libexec/scoop-bucket.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ param($cmd, $name, $repo)
2222
. "$psscriptroot\..\lib\core.ps1"
2323
. "$psscriptroot\..\lib\buckets.ps1"
2424
. "$psscriptroot\..\lib\help.ps1"
25-
. "$psscriptroot\..\lib\git.ps1"
2625

2726
reset_aliases
2827

libexec/scoop-status.ps1

+9-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ reset_aliases
1414
$currentdir = fullpath $(versiondir 'scoop' 'current')
1515
$needs_update = $false
1616

17-
if(test-path "$currentdir\.git") {
18-
Push-Location $currentdir
19-
git_fetch -q origin
20-
$commits = $(git log "HEAD..origin/$(scoop config SCOOP_BRANCH)" --oneline)
17+
if(Test-Path "$currentdir\.git") {
18+
git_cmd -C "`"$currentdir`"" fetch -q origin
19+
$commits = $(git -C "`"$currentdir`"" log "HEAD..origin/$(scoop config SCOOP_BRANCH)" --oneline)
2120
if($commits) { $needs_update = $true }
22-
Pop-Location
2321
}
2422
else {
2523
$needs_update = $true
@@ -39,7 +37,7 @@ $onhold = @()
3937
$true, $false | ForEach-Object { # local and global apps
4038
$global = $_
4139
$dir = appsdir $global
42-
if(!(test-path $dir)) { return }
40+
if(!(Test-Path $dir)) { return }
4341

4442
Get-ChildItem $dir | Where-Object name -ne 'scoop' | ForEach-Object {
4543
$app = $_.name
@@ -63,37 +61,37 @@ $true, $false | ForEach-Object { # local and global apps
6361
}
6462

6563
if($outdated) {
66-
write-host -f DarkCyan 'Updates are available for:'
64+
Write-Host -f DarkCyan 'Updates are available for:'
6765
$outdated.keys | ForEach-Object {
6866
$versions = $outdated.$_
6967
" $_`: $($versions[0]) -> $($versions[1])"
7068
}
7169
}
7270

7371
if($onhold) {
74-
write-host -f DarkCyan 'These apps are outdated and on hold:'
72+
Write-Host -f DarkCyan 'These apps are outdated and on hold:'
7573
$onhold.keys | ForEach-Object {
7674
$versions = $onhold.$_
7775
" $_`: $($versions[0]) -> $($versions[1])"
7876
}
7977
}
8078

8179
if($removed) {
82-
write-host -f DarkCyan 'These app manifests have been removed:'
80+
Write-Host -f DarkCyan 'These app manifests have been removed:'
8381
$removed.keys | ForEach-Object {
8482
" $_"
8583
}
8684
}
8785

8886
if($failed) {
89-
write-host -f DarkCyan 'These apps failed to install:'
87+
Write-Host -f DarkCyan 'These apps failed to install:'
9088
$failed.keys | ForEach-Object {
9189
" $_"
9290
}
9391
}
9492

9593
if($missing_deps) {
96-
write-host -f DarkCyan 'Missing runtime dependencies:'
94+
Write-Host -f DarkCyan 'Missing runtime dependencies:'
9795
$missing_deps | ForEach-Object {
9896
$app, $deps = $_
9997
" '$app' requires '$([string]::join("', '", $deps))'"

libexec/scoop-update.ps1

+23-28
Original file line numberDiff line numberDiff line change
@@ -63,61 +63,58 @@ function update_scoop() {
6363
# check for git
6464
if(!(Test-CommandAvailable git)) { abort "Scoop uses Git to update itself. Run 'scoop install git' and try again." }
6565

66-
write-host "Updating Scoop..."
66+
Write-Host "Updating Scoop..."
6767
$last_update = $(last_scoop_update)
6868
if ($null -eq $last_update) {$last_update = [System.DateTime]::Now}
6969
$last_update = $last_update.ToString('s')
7070
$show_update_log = get_config 'show_update_log' $true
7171
$currentdir = fullpath $(versiondir 'scoop' 'current')
72-
if (!(test-path "$currentdir\.git")) {
72+
if (!(Test-Path "$currentdir\.git")) {
7373
$newdir = fullpath $(versiondir 'scoop' 'new')
7474

7575
# get git scoop
7676
git_clone -q $configRepo --branch $configBranch --single-branch "`"$newdir`""
7777

7878
# check if scoop was successful downloaded
79-
if (!(test-path "$newdir")) {
79+
if (!(Test-Path "$newdir")) {
8080
abort 'Scoop update failed.'
8181
}
8282

8383
# replace non-git scoop with the git version
8484
Remove-Item -r -force $currentdir -ea stop
8585
Move-Item $newdir $currentdir
8686
} else {
87-
Push-Location $currentdir
88-
89-
$previousCommit = Invoke-Expression 'git rev-parse HEAD'
90-
$currentRepo = Invoke-Expression "git config remote.origin.url"
91-
$currentBranch = Invoke-Expression "git branch"
87+
$previousCommit = Invoke-Expression "git -C '$currentdir' rev-parse HEAD"
88+
$currentRepo = Invoke-Expression "git -C '$currentdir' config remote.origin.url"
89+
$currentBranch = Invoke-Expression "git -C '$currentdir' branch"
9290

9391
$isRepoChanged = !($currentRepo -match $configRepo)
9492
$isBranchChanged = !($currentBranch -match "\*\s+$configBranch")
9593

9694
# Change remote url if the repo is changed
9795
if ($isRepoChanged) {
98-
Invoke-Expression "git config remote.origin.url '$configRepo'"
96+
Invoke-Expression "git -C '$currentdir' config remote.origin.url '$configRepo'"
9997
}
10098

10199
# Fetch and reset local repo if the repo or the branch is changed
102100
if ($isRepoChanged -or $isBranchChanged) {
103101
# Reset git fetch refs, so that it can fetch all branches (GH-3368)
104-
Invoke-Expression "git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'"
102+
Invoke-Expression "git -C '$currentdir' config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'"
105103
# fetch remote branch
106-
git_fetch --force origin "refs/heads/`"$configBranch`":refs/remotes/origin/$configBranch" -q
104+
git_cmd -C "`"$currentdir`"" fetch --force origin "refs/heads/`"$configBranch`":refs/remotes/origin/$configBranch" -q
107105
# checkout and track the branch
108-
git_checkout -B $configBranch -t origin/$configBranch -q
106+
git_cmd -C "`"$currentdir`"" checkout -B $configBranch -t origin/$configBranch -q
109107
# reset branch HEAD
110-
Invoke-Expression "git reset --hard origin/$configBranch -q"
108+
Invoke-Expression "git -C '$currentdir' reset --hard origin/$configBranch -q"
111109
} else {
112-
git_pull -q
110+
git_cmd -C "`"$currentdir`"" pull -q
113111
}
114112

115113
$res = $lastexitcode
116114
if ($show_update_log) {
117-
Invoke-Expression "git --no-pager log --no-decorate --grep='^chore' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
115+
Invoke-Expression "git -C '$currentdir' --no-pager log --no-decorate --grep='^chore' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
118116
}
119117

120-
Pop-Location
121118
if ($res -ne 0) {
122119
abort 'Update failed.'
123120
}
@@ -150,13 +147,11 @@ function update_scoop() {
150147
continue
151148
}
152149

153-
Push-Location $bucketLoc
154-
$previousCommit = (Invoke-Expression 'git rev-parse HEAD')
155-
git_pull -q
150+
$previousCommit = (Invoke-Expression "git -C '$bucketLoc' rev-parse HEAD")
151+
git_cmd -C "`"$bucketLoc`"" pull -q
156152
if ($show_update_log) {
157-
Invoke-Expression "git --no-pager log --no-decorate --grep='^chore' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
153+
Invoke-Expression "git -C '$bucketLoc' --no-pager log --no-decorate --grep='^chore' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
158154
}
159-
Pop-Location
160155
}
161156

162157
set_config lastupdate ([System.DateTime]::Now.ToString('o')) | Out-Null
@@ -196,7 +191,7 @@ function update($app, $global, $quiet = $false, $independent, $suggested, $use_c
196191
return
197192
}
198193

199-
write-host "Updating '$app' ($old_version -> $version)"
194+
Write-Host "Updating '$app' ($old_version -> $version)"
200195

201196
# region Workaround
202197
# Workaround for https://github.com/ScoopInstaller/Scoop/issues/2220 until install is refactored
@@ -217,7 +212,7 @@ function update($app, $global, $quiet = $false, $independent, $suggested, $use_c
217212

218213
if (!$ok) {
219214
error $err
220-
if (test-path $source) {
215+
if (Test-Path $source) {
221216
# rm cached file
222217
Remove-Item -force $source
223218
}
@@ -244,7 +239,7 @@ function update($app, $global, $quiet = $false, $independent, $suggested, $use_c
244239
}
245240
#endregion Workaround for #2952
246241

247-
write-host "Uninstalling '$app' ($old_version)"
242+
Write-Host "Uninstalling '$app' ($old_version)"
248243
run_uninstaller $old_manifest $architecture $dir
249244
rm_shims $old_manifest $global $architecture
250245
env_rm_path $old_manifest $dir $global $architecture
@@ -319,7 +314,7 @@ if (-not ($apps -or $all)) {
319314
if ($status.installed -and ($force -or $status.outdated)) {
320315
if(!$status.hold) {
321316
$outdated += applist $app $global
322-
write-host -f yellow ("$app`: $($status.version) -> $($status.latest_version){0}" -f ('',' (global)')[$global])
317+
Write-Host -f yellow ("$app`: $($status.version) -> $($status.latest_version){0}" -f ('',' (global)')[$global])
323318
} else {
324319
warn "'$app' is held to version $($status.version)"
325320
}
@@ -338,11 +333,11 @@ if (-not ($apps -or $all)) {
338333
warn "To disable this warning, run 'scoop config aria2-warning-enabled false'."
339334
}
340335
if ($outdated.Length -gt 1) {
341-
write-host -f DarkCyan "Updating $($outdated.Length) outdated apps:"
336+
Write-Host -f DarkCyan "Updating $($outdated.Length) outdated apps:"
342337
} elseif ($outdated.Length -eq 0) {
343-
write-host -f Green "Latest versions for all apps are installed! For more information try 'scoop status'"
338+
Write-Host -f Green "Latest versions for all apps are installed! For more information try 'scoop status'"
344339
} else {
345-
write-host -f DarkCyan "Updating one outdated app:"
340+
Write-Host -f DarkCyan "Updating one outdated app:"
346341
}
347342
}
348343

0 commit comments

Comments
 (0)