diff --git a/.includes/cmdline.sh b/.includes/cmdline.sh index 2567eef63c..13fc2d3a06 100644 --- a/.includes/cmdline.sh +++ b/.includes/cmdline.sh @@ -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 result=$? ;; diff --git a/.includes/ds_functions.sh b/.includes/ds_functions.sh index e9faa0d224..b83643fa4b 100644 --- a/.includes/ds_functions.sh +++ b/.includes/ds_functions.sh @@ -6,32 +6,23 @@ 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-}" } 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} } @@ -39,10 +30,8 @@ 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} } @@ -50,19 +39,15 @@ 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}")" @@ -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 @@ -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 @@ -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} } @@ -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 @@ -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 @@ -264,7 +242,6 @@ git_best_branch() { BestBranch="${DefaultBranch}" fi echo "${BestBranch}" - popd &> /dev/null } ds_best_branch() { diff --git a/.scripts/update_self.sh b/.scripts/update_self.sh index 51e74a1ed5..b49358d803 100644 --- a/.scripts/update_self.sh +++ b/.scripts/update_self.sh @@ -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 @@ -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)" @@ -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 diff --git a/.scripts/update_templates.sh b/.scripts/update_templates.sh index f7acae7e4f..6ee4333dd6 100644 --- a/.scripts/update_templates.sh +++ b/.scripts/update_templates.sh @@ -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 @@ -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." @@ -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() {