Skip to content

Commit 2396d1f

Browse files
committed
Use git -C Folder command instead of pushd Folder / git command / popd
1 parent 6c11a07 commit 2396d1f

File tree

3 files changed

+37
-80
lines changed

3 files changed

+37
-80
lines changed

.includes/ds_functions.sh

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,48 @@ git_branch() {
66
local GitPath=${1}
77
local DefaultBranch=${2-}
88
local LegacyBranch=${3-}
9-
pushd "${GitPath}" &> /dev/null ||
10-
fatal \
11-
"Failed to change directory." \
12-
"Failing command: ${C["FailingCommand"]}pushd \"${GitPath}\""
13-
git fetch --quiet --tags &> /dev/null || true
14-
git symbolic-ref --short HEAD 2> /dev/null || git describe --tags --exact-match 2> /dev/null || git_best_branch "${GitPath}" "${DefaultBranch-}" "${LegacyBranch-}"
15-
popd &> /dev/null
9+
git -C "${GitPath}" fetch --quiet --tags &> /dev/null || true
10+
git -C "${GitPath}" symbolic-ref --short HEAD 2> /dev/null || git -C "${GitPath}" describe --tags --exact-match 2> /dev/null || git_best_branch "${GitPath}" "${DefaultBranch-}" "${LegacyBranch-}"
1611
}
1712

1813
git_branch_exists() {
1914
local GitPath=${1}
2015
local Branch=${2-}
21-
pushd "${GitPath}" &> /dev/null || return 1
22-
git ls-remote --quiet --exit-code --heads origin "${Branch}" &> /dev/null
16+
git -C "${GitPath}" ls-remote --quiet --exit-code --heads origin "${Branch}" &> /dev/null
2317
local result=$?
24-
popd &> /dev/null
2518
return ${result}
2619
}
2720

2821
git_tag_exists() {
2922
local GitPath=${1}
3023
local Tag=${2-}
31-
pushd "${GitPath}" &> /dev/null || return 1
32-
git ls-remote --quiet --exit-code --tags origin "${Tag}" &> /dev/null
24+
git -C "${GitPath}" ls-remote --quiet --exit-code --tags origin "${Tag}" &> /dev/null
3325
local result=$?
34-
popd &> /dev/null
3526
return ${result}
3627
}
3728

3829
git_commit_exists() {
3930
local GitPath=${1}
4031
local Commit=${2-}
4132
[[ ${Commit} =~ ^[0-9a-fA-F]{7,40}$ ]] || return 1
42-
pushd "${GitPath}" &> /dev/null || return 1
43-
git rev-parse --quiet --verify "${Commit}^{commit}" &> /dev/null
33+
git -C "${GitPath}" rev-parse --quiet --verify "${Commit}^{commit}" &> /dev/null
4434
local result=$?
45-
popd &> /dev/null
4635
return ${result}
4736
}
4837

4938
git_version() {
5039
local GitPath=${1}
5140
local CheckBranch=${2-}
5241
local commitish Branch result
53-
pushd "${GitPath}" &> /dev/null ||
54-
fatal \
55-
"Failed to change directory." \
56-
"Failing command: ${C["FailingCommand"]}pushd \"${GitPath}\""
5742

5843
if [[ -n ${CheckBranch-} ]]; then
59-
if git show-ref --quiet --tags "${CheckBranch}" &> /dev/null; then
44+
if git -C "${GitPath}" show-ref --quiet --tags "${CheckBranch}" &> /dev/null; then
6045
commitish="${CheckBranch}"
6146
Branch="${CheckBranch}"
62-
elif git ls-remote --quiet --exit-code --heads origin "${CheckBranch}" &> /dev/null; then
47+
elif git -C "${GitPath}" ls-remote --quiet --exit-code --heads origin "${CheckBranch}" &> /dev/null; then
6348
commitish="origin/${CheckBranch}"
6449
Branch="${CheckBranch}"
65-
elif git rev-parse --quiet --verify "${CheckBranch}^{commit}" &> /dev/null; then
50+
elif git -C "${GitPath}" rev-parse --quiet --verify "${CheckBranch}^{commit}" &> /dev/null; then
6651
commitish="${CheckBranch}"
6752
# Try to find a branch name if we were given a SHA
6853
Branch="$(git_best_branch "${GitPath}")"
@@ -88,14 +73,14 @@ git_version() {
8873
if [[ -z ${CheckBranch-} ]] || git_branch_exists "${GitPath}" "${Branch}" || git_tag_exists "${GitPath}" "${Branch}" || git_commit_exists "${GitPath}" "${Branch}"; then
8974
# Get the current tag. If no tag, use the commit instead.
9075
local VersionString
91-
VersionString="$(git describe --tags --exact-match "${commitish}" 2> /dev/null || true)"
76+
VersionString="$(git -C "${GitPath}" describe --tags --exact-match "${commitish}" 2> /dev/null || true)"
9277
if [[ -n ${VersionString-} ]]; then
9378
if [[ ${VersionString} != "${Branch}" ]] && [[ ${Branch} != "main" || ${VersionString} == "main" ]]; then
9479
VersionString="${Branch} ${VersionString}"
9580
fi
9681
else
9782
local CommitHash
98-
CommitHash="$(git rev-parse --short "${commitish}" 2> /dev/null || true)"
83+
CommitHash="$(git -C "${GitPath}" rev-parse --short "${commitish}" 2> /dev/null || true)"
9984
if [[ ${CommitHash} == "${Branch}" ]]; then
10085
VersionString="commit ${CommitHash}"
10186
else
@@ -106,21 +91,16 @@ git_version() {
10691
VersionString=''
10792
fi
10893
echo "${VersionString}"
109-
popd &> /dev/null
11094
}
11195

11296
git_update_available() {
11397
local GitPath=${1}
11498
local CurrentRef=${2-}
11599
local TargetRef=${3-}
116-
pushd "${GitPath}" &> /dev/null ||
117-
fatal \
118-
"Failed to change directory." \
119-
"Failing command: ${C["FailingCommand"]}pushd \"${GitPath}\""
120-
git fetch --quiet --tags &> /dev/null || true
100+
git -C "${GitPath}" fetch --quiet --tags &> /dev/null || true
121101
local -i result=0
122102
local Current Remote
123-
Current=$(git rev-parse HEAD 2> /dev/null || true)
103+
Current=$(git -C "${GitPath}" rev-parse HEAD 2> /dev/null || true)
124104

125105
if [[ -n ${CurrentRef} ]]; then
126106
if [[ -z ${TargetRef-} ]]; then
@@ -129,23 +109,22 @@ git_update_available() {
129109

130110
# If CurrentRef is a tag, we compare against TargetRef (usually a branch)
131111
# Even if it's a tag, we compare the current hash against the target branch/ref hash
132-
Remote=$(git rev-parse "origin/${TargetRef}" 2> /dev/null || true)
112+
Remote=$(git -C "${GitPath}" rev-parse "origin/${TargetRef}" 2> /dev/null || true)
133113
[[ ${Current} != "${Remote}" ]]
134114
result=$?
135115
else
136116
# Default behavior: compare HEAD to upstream
137-
Remote=$(git rev-parse '@{u}' 2> /dev/null || true)
117+
Remote=$(git -C "${GitPath}" rev-parse '@{u}' 2> /dev/null || true)
138118
if [[ -n ${Remote} ]]; then
139119
[[ ${Current} != "${Remote}" ]]
140120
result=$?
141121
else
142122
# No upstream (detached), check against origin/main as fallback
143-
Remote=$(git rev-parse "origin/main" 2> /dev/null || true)
123+
Remote=$(git -C "${GitPath}" rev-parse "origin/main" 2> /dev/null || true)
144124
[[ ${Current} != "${Remote}" ]]
145125
result=$?
146126
fi
147127
fi
148-
popd &> /dev/null
149128
return ${result}
150129
}
151130

@@ -224,10 +203,9 @@ git_best_branch() {
224203
local GitPath=${1}
225204
local DefaultBranch=${2-}
226205
local LegacyBranch=${3-}
227-
pushd "${GitPath}" &> /dev/null || return 1
228206
local -a Branches=()
229207
# Get remote branches containing current HEAD
230-
readarray -t Branches < <(git branch -r --contains HEAD 2> /dev/null | sed 's/^[[:space:]]*origin\///' | grep -v 'HEAD ->')
208+
readarray -t Branches < <(git -C "${GitPath}" branch -r --contains HEAD 2> /dev/null | sed 's/^[[:space:]]*origin\///' | grep -v 'HEAD ->')
231209
local BestBranch=""
232210
if [[ ${#Branches[@]} -gt 0 ]]; then
233211
# 1. Prioritize Default Branch
@@ -254,7 +232,7 @@ git_best_branch() {
254232

255233
# 4. If we ended up with the Legacy Branch but the Default Branch exists, force use of Default Branch
256234
if [[ ${BestBranch} == "${LegacyBranch}" ]] && [[ -n ${DefaultBranch} ]]; then
257-
if git show-ref --quiet --verify "refs/remotes/origin/${DefaultBranch}"; then
235+
if git -C "${GitPath}" show-ref --quiet --verify "refs/remotes/origin/${DefaultBranch}"; then
258236
BestBranch="${DefaultBranch}"
259237
fi
260238
fi
@@ -264,7 +242,6 @@ git_best_branch() {
264242
BestBranch="${DefaultBranch}"
265243
fi
266244
echo "${BestBranch}"
267-
popd &> /dev/null
268245
}
269246

270247
ds_best_branch() {

.scripts/update_self.sh

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ update_self() {
1414
local Title="Update ${APPLICATION_NAME}"
1515
local Question YesNotice NoNotice
1616

17-
RunAndLog "" BothToNull \
18-
fatal "Failed to change directory." \
19-
pushd "${SCRIPTPATH}"
20-
2117
if [[ -z ${Branch-} ]]; then
2218
Branch="$(ds_branch)"
2319
if ds_tag_exists "${Branch-}"; then
@@ -42,7 +38,6 @@ update_self() {
4238
NoNotice="${C["ApplicationName"]-}${APPLICATION_NAME}${NC-} will not be updated."
4339
YesNotice="Updating ${C["ApplicationName"]-}${APPLICATION_NAME}${NC-} from '${C["Version"]}${CurrentVersion}${NC}' to '${C["Version"]}${RemoteVersion}${NC}'"
4440
fi
45-
popd &> /dev/null
4641

4742
local CommandLineText
4843
CommandLineText="$(printf '%q ' "${APPLICATION_COMMAND}" "--update" "$@" | xargs)"
@@ -109,48 +104,43 @@ commands_update_self_logic() {
109104
local Notice=${2-}
110105
shift 2
111106

112-
RunAndLog "" "BothToNull" \
113-
fatal "Failed to change directory." \
114-
pushd "${SCRIPTPATH}"
115-
116107
notice "${Notice}"
117108

118109
info "Setting file ownership on current repository files"
119110
sudo chown -R "$(id -u)":"$(id -g)" "${SCRIPTPATH}/.git" &> /dev/null || true
120111
sudo chown "$(id -u)":"$(id -g)" "${SCRIPTPATH}" &> /dev/null || true
121-
git ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true
112+
git -C "${SCRIPTPATH}" ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true
122113

123114
info "Fetching recent changes from git."
124115
RunAndLog info "git:info" \
125116
fatal "Failed to fetch recent changes from git." \
126-
git fetch --all --prune -v
117+
git -C "${SCRIPTPATH}" fetch --all --prune -v
127118
if [[ ${CI-} != true ]]; then
128119
RunAndLog info "git:info" \
129120
fatal "Failed to switch to github ref '${C["Branch"]}${Branch}${NC}'." \
130-
git checkout --force "${Branch}"
121+
git -C "${SCRIPTPATH}" checkout --force "${Branch}"
131122

132123
# If it's a branch (not a tag or SHA), perform reset and pull
133-
if git ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
124+
if git -C "${SCRIPTPATH}" ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
134125
RunAndLog info "git:info" \
135126
fatal "Failed to reset to branch '${C["Branch"]}origin/${Branch}${NC}'." \
136-
git reset --hard origin/"${Branch}"
127+
git -C "${SCRIPTPATH}" reset --hard origin/"${Branch}"
137128
info "Pulling recent changes from git."
138129
RunAndLog info "git:info" \
139130
fatal "Failed to pull recent changes from git." \
140-
git pull
131+
git -C "${SCRIPTPATH}" pull
141132
fi
142133
fi
143134
info \
144135
"Cleaning up unnecessary files and optimizing the local repository." \
145-
"$(git gc 2>&1 || true)"
136+
"$(git -C "${SCRIPTPATH}" gc 2>&1 || true)"
146137
info \
147-
"Setting file ownership on new repository files" \
148-
"$(git ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true)" \
149-
"$(sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}/.git" &> /dev/null || true)" \
150-
"$(sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}" &> /dev/null || true)"
138+
"Setting file ownership on new repository files"
139+
git -C "${SCRIPTPATH}" ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
140+
sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}/.git" &> /dev/null || true
141+
sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}" &> /dev/null || true
151142
notice \
152143
"Updated ${APPLICATION_NAME} to '${C["Version"]}$(ds_version)${NC}'"
153-
popd &> /dev/null
154144

155145
# run_script 'reset_needs' # Add script lines in-line below
156146
if [[ -d ${TIMESTAMPS_FOLDER:?} ]]; then

.scripts/update_templates.sh

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ update_templates() {
1111
local Title="Update ${TargetName}"
1212
local Question YesNotice NoNotice
1313

14-
RunAndLog "" "BothToNull" \
15-
fatal "Failed to change directory." \
16-
pushd "${TEMPLATES_PARENT_FOLDER}"
17-
1814
if [[ -z ${Branch-} ]]; then
1915
Branch="$(templates_branch)"
2016
if templates_tag_exists "${Branch-}"; then
@@ -39,7 +35,6 @@ update_templates() {
3935
NoNotice="${C["ApplicationName"]-}${TargetName}${NC-} will not be updated."
4036
YesNotice="Updating ${C["ApplicationName"]-}${TargetName}${NC-} from '${C["Version"]}${CurrentVersion}${NC}' to '${C["Version"]}${RemoteVersion}${NC}'"
4137
fi
42-
popd &> /dev/null
4338

4439
if ! templates_branch_exists "${Branch}" && ! templates_tag_exists "${Branch}" && ! templates_commit_exists "${Branch}"; then
4540
local ErrorMessage="${C["ApplicationName"]-}${TargetName}${NC-} ref '${C["Branch"]}${Branch}${NC}' does not exist on origin."
@@ -92,46 +87,41 @@ commands_update_templates() {
9287
local Notice=${2-}
9388
shift 2
9489

95-
RunAndLog "" "BothToNull" \
96-
fatal "Failed to change directory." \
97-
pushd "${TEMPLATES_PARENT_FOLDER}"
98-
9990
notice "${Notice}"
10091
info "Setting file ownership on current repository files"
10192
sudo chown -R "$(id -u)":"$(id -g)" "${TEMPLATES_PARENT_FOLDER}/.git" &> /dev/null || true
10293
sudo chown "$(id -u)":"$(id -g)" "${TEMPLATES_PARENT_FOLDER}" &> /dev/null || true
103-
git ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true
94+
git -C "${TEMPLATES_PARENT_FOLDER}" ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true
10495

10596
info "Fetching recent changes from git."
10697
RunAndLog info "git:info" \
10798
fatal "Failed to fetch recent changes from git." \
108-
git fetch --all --prune -v
99+
git -C "${TEMPLATES_PARENT_FOLDER}" fetch --all --prune -v
109100
if [[ ${CI-} != true ]]; then
110101
RunAndLog info "git:info" \
111102
fatal "Failed to switch to github ref '${C["Branch"]}${Branch}${NC}'." \
112-
git checkout --force "${Branch}"
103+
git -C "${TEMPLATES_PARENT_FOLDER}" checkout --force "${Branch}"
113104

114105
# If it's a branch (not a tag or SHA), perform reset and pull
115-
if git ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
106+
if git -C "${TEMPLATES_PARENT_FOLDER}" ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
116107
RunAndLog info "git:info" \
117108
fatal "Failed to reset to branch '${C["Branch"]}origin/${Branch}${NC}'." \
118-
git reset --hard origin/"${Branch}"
109+
git -C "${TEMPLATES_PARENT_FOLDER}" reset --hard origin/"${Branch}"
119110
info "Pulling recent changes from git."
120111
RunAndLog info "git:info" \
121112
fatal "Failed to pull recent changes from git." \
122-
git pull
113+
git -C "${TEMPLATES_PARENT_FOLDER}" pull
123114
fi
124115
fi
125116
info "Cleaning up unnecessary files and optimizing the local repository."
126-
info "$(git gc 2>&1 || true)"
117+
git -C "${TEMPLATES_PARENT_FOLDER}" gc 2>&1 || true
127118
info "Setting file ownership on new repository files"
128-
git ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
119+
git -C "${TEMPLATES_PARENT_FOLDER}" ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
129120
sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${TEMPLATES_PARENT_FOLDER}/.git" &> /dev/null || true
130121
sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${TEMPLATES_PARENT_FOLDER}" &> /dev/null || true
131122
notice "Updated ${TargetName} to '${C["Version"]}$(templates_version)${NC}'"
132-
popd &> /dev/null
133123

134-
run_script 'reset_needs' # Add script lines in-line below
124+
run_script 'reset_needs'
135125
}
136126

137127
test_update_templates() {

0 commit comments

Comments
 (0)