-
-
Notifications
You must be signed in to change notification settings - Fork 291
Use git -C Folder command instead of pushd Folder / git command…
#2419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| 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}")" | ||
|
|
@@ -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() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Using
|| truehere forcesresultto always be 0 and hides update failures.Because of the
|| true, the last command in each pipeline becomestrue, so$?(and thusresult) is always 0, regardless of whetherupdate_templatesorupdate_selffailed. Previously,resultreflectedupdate_self’s success/failure. If you want to avoid aborting but still return a meaningful status, capture each exit code before|| trueand combine them, or at least don’t use|| trueon the call whose exit code you need to inspect.