Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .includes/cmdline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ run_command() {
-u | --update)
local AppBranch="${ParamsArray[0]-}"
local TemplatesBranch="${ParamsArray[1]-}"
run_script 'update_templates' "${TemplatesBranch-}"
run_script 'update_self' "${AppBranch-}" "${REST_OF_ARGS_ARRAY[@]}"
run_script 'update_templates' "${TemplatesBranch-}" || true
run_script 'update_self' "${AppBranch-}" "${REST_OF_ARGS_ARRAY[@]}" || true
Comment on lines +668 to +669
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Using || true here forces result to always be 0 and hides update failures.

Because of the || true, the last command in each pipeline becomes true, so $? (and thus result) is always 0, regardless of whether update_templates or update_self failed. Previously, result reflected update_self’s success/failure. If you want to avoid aborting but still return a meaningful status, capture each exit code before || true and combine them, or at least don’t use || true on the call whose exit code you need to inspect.

result=$?
;;

Expand Down
57 changes: 17 additions & 40 deletions .includes/ds_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,48 @@ git_branch() {
local GitPath=${1}
local DefaultBranch=${2-}
local LegacyBranch=${3-}
pushd "${GitPath}" &> /dev/null ||
fatal \
"Failed to change directory." \
"Failing command: ${C["FailingCommand"]}pushd \"${GitPath}\""
git fetch --quiet --tags &> /dev/null || true
git symbolic-ref --short HEAD 2> /dev/null || git describe --tags --exact-match 2> /dev/null || git_best_branch "${GitPath}" "${DefaultBranch-}" "${LegacyBranch-}"
popd &> /dev/null
git -C "${GitPath}" fetch --quiet --tags &> /dev/null || true
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-}"
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Loss of explicit fatal behavior when the git path is invalid or inaccessible.

With pushd "${GitPath}", an invalid or inaccessible repo path caused fatal and stopped execution. Using git -C "${GitPath}" now lets those failures fall through, potentially yielding empty or misleading results instead of a hard failure. If you still want invalid GitPath to be a hard error, add an explicit check that GitPath is a valid repo (or that git -C succeeds) and call fatal/return non‑zero when it isn’t.

}

git_branch_exists() {
local GitPath=${1}
local Branch=${2-}
pushd "${GitPath}" &> /dev/null || return 1
git ls-remote --quiet --exit-code --heads origin "${Branch}" &> /dev/null
git -C "${GitPath}" ls-remote --quiet --exit-code --heads origin "${Branch}" &> /dev/null
local result=$?
popd &> /dev/null
return ${result}
}

git_tag_exists() {
local GitPath=${1}
local Tag=${2-}
pushd "${GitPath}" &> /dev/null || return 1
git ls-remote --quiet --exit-code --tags origin "${Tag}" &> /dev/null
git -C "${GitPath}" ls-remote --quiet --exit-code --tags origin "${Tag}" &> /dev/null
local result=$?
popd &> /dev/null
return ${result}
}

git_commit_exists() {
local GitPath=${1}
local Commit=${2-}
[[ ${Commit} =~ ^[0-9a-fA-F]{7,40}$ ]] || return 1
pushd "${GitPath}" &> /dev/null || return 1
git rev-parse --quiet --verify "${Commit}^{commit}" &> /dev/null
git -C "${GitPath}" rev-parse --quiet --verify "${Commit}^{commit}" &> /dev/null
local result=$?
popd &> /dev/null
return ${result}
}

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

if [[ -n ${CheckBranch-} ]]; then
if git show-ref --quiet --tags "${CheckBranch}" &> /dev/null; then
if git -C "${GitPath}" show-ref --quiet --tags "${CheckBranch}" &> /dev/null; then
commitish="${CheckBranch}"
Branch="${CheckBranch}"
elif git ls-remote --quiet --exit-code --heads origin "${CheckBranch}" &> /dev/null; then
elif git -C "${GitPath}" ls-remote --quiet --exit-code --heads origin "${CheckBranch}" &> /dev/null; then
commitish="origin/${CheckBranch}"
Branch="${CheckBranch}"
elif git rev-parse --quiet --verify "${CheckBranch}^{commit}" &> /dev/null; then
elif git -C "${GitPath}" rev-parse --quiet --verify "${CheckBranch}^{commit}" &> /dev/null; then
commitish="${CheckBranch}"
# Try to find a branch name if we were given a SHA
Branch="$(git_best_branch "${GitPath}")"
Expand All @@ -88,14 +73,14 @@ git_version() {
if [[ -z ${CheckBranch-} ]] || git_branch_exists "${GitPath}" "${Branch}" || git_tag_exists "${GitPath}" "${Branch}" || git_commit_exists "${GitPath}" "${Branch}"; then
# Get the current tag. If no tag, use the commit instead.
local VersionString
VersionString="$(git describe --tags --exact-match "${commitish}" 2> /dev/null || true)"
VersionString="$(git -C "${GitPath}" describe --tags --exact-match "${commitish}" 2> /dev/null || true)"
if [[ -n ${VersionString-} ]]; then
if [[ ${VersionString} != "${Branch}" ]] && [[ ${Branch} != "main" || ${VersionString} == "main" ]]; then
VersionString="${Branch} ${VersionString}"
fi
else
local CommitHash
CommitHash="$(git rev-parse --short "${commitish}" 2> /dev/null || true)"
CommitHash="$(git -C "${GitPath}" rev-parse --short "${commitish}" 2> /dev/null || true)"
if [[ ${CommitHash} == "${Branch}" ]]; then
VersionString="commit ${CommitHash}"
else
Expand All @@ -106,21 +91,16 @@ git_version() {
VersionString=''
fi
echo "${VersionString}"
popd &> /dev/null
}

git_update_available() {
local GitPath=${1}
local CurrentRef=${2-}
local TargetRef=${3-}
pushd "${GitPath}" &> /dev/null ||
fatal \
"Failed to change directory." \
"Failing command: ${C["FailingCommand"]}pushd \"${GitPath}\""
git fetch --quiet --tags &> /dev/null || true
git -C "${GitPath}" fetch --quiet --tags &> /dev/null || true
local -i result=0
local Current Remote
Current=$(git rev-parse HEAD 2> /dev/null || true)
Current=$(git -C "${GitPath}" rev-parse HEAD 2> /dev/null || true)

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

# If CurrentRef is a tag, we compare against TargetRef (usually a branch)
# Even if it's a tag, we compare the current hash against the target branch/ref hash
Remote=$(git rev-parse "origin/${TargetRef}" 2> /dev/null || true)
Remote=$(git -C "${GitPath}" rev-parse "origin/${TargetRef}" 2> /dev/null || true)
[[ ${Current} != "${Remote}" ]]
result=$?
else
# Default behavior: compare HEAD to upstream
Remote=$(git rev-parse '@{u}' 2> /dev/null || true)
Remote=$(git -C "${GitPath}" rev-parse '@{u}' 2> /dev/null || true)
if [[ -n ${Remote} ]]; then
[[ ${Current} != "${Remote}" ]]
result=$?
else
# No upstream (detached), check against origin/main as fallback
Remote=$(git rev-parse "origin/main" 2> /dev/null || true)
Remote=$(git -C "${GitPath}" rev-parse "origin/main" 2> /dev/null || true)
[[ ${Current} != "${Remote}" ]]
result=$?
fi
fi
popd &> /dev/null
return ${result}
}

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

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

ds_best_branch() {
Expand Down
38 changes: 14 additions & 24 deletions .scripts/update_self.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ update_self() {
local Title="Update ${APPLICATION_NAME}"
local Question YesNotice NoNotice

RunAndLog "" BothToNull \
fatal "Failed to change directory." \
pushd "${SCRIPTPATH}"

if [[ -z ${Branch-} ]]; then
Branch="$(ds_branch)"
if ds_tag_exists "${Branch-}"; then
Expand All @@ -42,7 +38,6 @@ update_self() {
NoNotice="${C["ApplicationName"]-}${APPLICATION_NAME}${NC-} will not be updated."
YesNotice="Updating ${C["ApplicationName"]-}${APPLICATION_NAME}${NC-} from '${C["Version"]}${CurrentVersion}${NC}' to '${C["Version"]}${RemoteVersion}${NC}'"
fi
popd &> /dev/null

local CommandLineText
CommandLineText="$(printf '%q ' "${APPLICATION_COMMAND}" "--update" "$@" | xargs)"
Expand Down Expand Up @@ -109,48 +104,43 @@ commands_update_self_logic() {
local Notice=${2-}
shift 2

RunAndLog "" "BothToNull" \
fatal "Failed to change directory." \
pushd "${SCRIPTPATH}"

notice "${Notice}"

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

info "Fetching recent changes from git."
RunAndLog info "git:info" \
fatal "Failed to fetch recent changes from git." \
git fetch --all --prune -v
git -C "${SCRIPTPATH}" fetch --all --prune -v
if [[ ${CI-} != true ]]; then
RunAndLog info "git:info" \
fatal "Failed to switch to github ref '${C["Branch"]}${Branch}${NC}'." \
git checkout --force "${Branch}"
git -C "${SCRIPTPATH}" checkout --force "${Branch}"

# If it's a branch (not a tag or SHA), perform reset and pull
if git ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
if git -C "${SCRIPTPATH}" ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
RunAndLog info "git:info" \
fatal "Failed to reset to branch '${C["Branch"]}origin/${Branch}${NC}'." \
git reset --hard origin/"${Branch}"
git -C "${SCRIPTPATH}" reset --hard origin/"${Branch}"
info "Pulling recent changes from git."
RunAndLog info "git:info" \
fatal "Failed to pull recent changes from git." \
git pull
git -C "${SCRIPTPATH}" pull
fi
fi
info \
"Cleaning up unnecessary files and optimizing the local repository." \
"$(git gc 2>&1 || true)"
info \
"Setting file ownership on new repository files" \
"$(git ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true)" \
"$(sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}/.git" &> /dev/null || true)" \
"$(sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}" &> /dev/null || true)"
info "Cleaning up unnecessary files and optimizing the local repository."
RunAndLog info "git:info" \
"" "" \
git -C "${SCRIPTPATH}" gc || true
info "Setting file ownership on new repository files"
git -C "${SCRIPTPATH}" ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}/.git" &> /dev/null || true
sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${SCRIPTPATH}" &> /dev/null || true
notice \
"Updated ${APPLICATION_NAME} to '${C["Version"]}$(ds_version)${NC}'"
popd &> /dev/null

# run_script 'reset_needs' # Add script lines in-line below
if [[ -d ${TIMESTAMPS_FOLDER:?} ]]; then
Expand Down
30 changes: 11 additions & 19 deletions .scripts/update_templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ update_templates() {
local Title="Update ${TargetName}"
local Question YesNotice NoNotice

RunAndLog "" "BothToNull" \
fatal "Failed to change directory." \
pushd "${TEMPLATES_PARENT_FOLDER}"

if [[ -z ${Branch-} ]]; then
Branch="$(templates_branch)"
if templates_tag_exists "${Branch-}"; then
Expand All @@ -39,7 +35,6 @@ update_templates() {
NoNotice="${C["ApplicationName"]-}${TargetName}${NC-} will not be updated."
YesNotice="Updating ${C["ApplicationName"]-}${TargetName}${NC-} from '${C["Version"]}${CurrentVersion}${NC}' to '${C["Version"]}${RemoteVersion}${NC}'"
fi
popd &> /dev/null

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

RunAndLog "" "BothToNull" \
fatal "Failed to change directory." \
pushd "${TEMPLATES_PARENT_FOLDER}"

notice "${Notice}"
info "Setting file ownership on current repository files"
sudo chown -R "$(id -u)":"$(id -g)" "${TEMPLATES_PARENT_FOLDER}/.git" &> /dev/null || true
sudo chown "$(id -u)":"$(id -g)" "${TEMPLATES_PARENT_FOLDER}" &> /dev/null || true
git ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true
git -C "${TEMPLATES_PARENT_FOLDER}" ls-tree -rt --name-only HEAD | xargs sudo chown "$(id -u)":"$(id -g)" &> /dev/null || true

info "Fetching recent changes from git."
RunAndLog info "git:info" \
fatal "Failed to fetch recent changes from git." \
git fetch --all --prune -v
git -C "${TEMPLATES_PARENT_FOLDER}" fetch --all --prune -v
if [[ ${CI-} != true ]]; then
RunAndLog info "git:info" \
fatal "Failed to switch to github ref '${C["Branch"]}${Branch}${NC}'." \
git checkout --force "${Branch}"
git -C "${TEMPLATES_PARENT_FOLDER}" checkout --force "${Branch}"

# If it's a branch (not a tag or SHA), perform reset and pull
if git ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
if git -C "${TEMPLATES_PARENT_FOLDER}" ls-remote --exit-code --heads origin "${Branch}" &> /dev/null; then
RunAndLog info "git:info" \
fatal "Failed to reset to branch '${C["Branch"]}origin/${Branch}${NC}'." \
git reset --hard origin/"${Branch}"
git -C "${TEMPLATES_PARENT_FOLDER}" reset --hard origin/"${Branch}"
info "Pulling recent changes from git."
RunAndLog info "git:info" \
fatal "Failed to pull recent changes from git." \
git pull
git -C "${TEMPLATES_PARENT_FOLDER}" pull
fi
fi
info "Cleaning up unnecessary files and optimizing the local repository."
info "$(git gc 2>&1 || true)"
RunAndLog info "git:info" \
"" "" \
git -C "${TEMPLATES_PARENT_FOLDER}" gc || true
info "Setting file ownership on new repository files"
git ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
git -C "${TEMPLATES_PARENT_FOLDER}" ls-tree -rt --name-only "${Branch}" | xargs sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" &> /dev/null || true
sudo chown -R "${DETECTED_PUID}":"${DETECTED_PGID}" "${TEMPLATES_PARENT_FOLDER}/.git" &> /dev/null || true
sudo chown "${DETECTED_PUID}":"${DETECTED_PGID}" "${TEMPLATES_PARENT_FOLDER}" &> /dev/null || true
notice "Updated ${TargetName} to '${C["Version"]}$(templates_version)${NC}'"
popd &> /dev/null

run_script 'reset_needs' # Add script lines in-line below
run_script 'reset_needs'
}

test_update_templates() {
Expand Down