Skip to content

Commit

Permalink
fix: reset remote if URL changed
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Apr 21, 2024
1 parent 093fbf3 commit 39d8810
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions libexec/decomposer/decomposer-install
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ install_library() {

cd "${library_target_dir}" || return 2

local revision revision_type revision_alt_type revision_reset
local revision revision_type revision_alt_type revision_reset default_remote

revision=$( jq -r '.revision' <<< "${object}" )
if [ "${revision}" = "null" ]; then
Expand All @@ -94,18 +94,26 @@ install_library() {
revision_alt_type=$( git cat-file -t "v${revision}" 2> /dev/null )
revision_reset=

if git branch -a | grep -q "/origin/${revision}"; then
revision_reset="origin/${revision}"
# use the origin for master as the default origin
# TODO: determine the default remote
default_remote="origin"
if git remote get-url "$default_remote" | grep -q "${url}"; then
printf -v "${status_text_variable}" 'remote URL has changed'
git remote set-url "$default_remote" "${url}"
fi

if git branch -a | grep -q "/${default_remote}/${revision}"; then
revision_reset="${default_remote}/${revision}"
elif [ "${revision_type}" = 'commit' ] || [ "${revision_type}" = 'tag' ]; then
revision_reset="${revision}"
elif [ "${revision_alt_type}" = 'commit' ] || [ "${revision_alt_type}" = 'tag' ]; then
revision_reset="v${revision}"
fi

if [ -z "${just_cloned}" ] && [ "${revision_reset}" = "origin/${revision}" ] || [ -z "${revision_reset}" ]; then
if [ -z "${just_cloned}" ] && [ "${revision_reset}" = "${default_remote}/${revision}" ] || [ -z "${revision_reset}" ]; then
# no need to fetch changes if we just cloned the repository
# or we can already resolve a commit or tag
if ! git fetch origin &> /dev/null; then
if ! git fetch ${default_remote} &> /dev/null; then
printf -v "${status_text_variable}" 'fetching changes failed'
return 1
fi
Expand All @@ -116,8 +124,8 @@ install_library() {
revision_alt_type=$( git cat-file -t "v${revision}" 2> /dev/null )
revision_reset=

if git branch -a | grep -q "/origin/${revision}"; then
revision_reset="origin/${revision}"
if git branch -a | grep -q "/${default_remote}/${revision}"; then
revision_reset="${default_remote}/${revision}"
elif [ "${revision_type}" = 'commit' ] || [ "${revision_type}" = 'tag' ]; then
revision_reset="${revision}"
elif [ "${revision_alt_type}" = 'commit' ] || [ "${revision_alt_type}" = 'tag' ]; then
Expand Down
18 changes: 18 additions & 0 deletions tests/install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ SUITE_NAME=$( test_suite_name )
assert_lib_installed Alpha-1.0 "${tag_alpha_lib_revision_hash}"
}

@test "${SUITE_NAME}: existing library resets upstream" {
create_decomposer_json alpha_tag_version

create_repository alpha-lib

# create usual clone of library
git clone "${TEST_REPOS_DIR}/alpha-lib" "${DECOMPOSER_TARGET_DIR}/Alpha-1.0"

# create new commit in repository and tag
git -C "${TEST_REPOS_DIR}/alpha-lib" remote set-url origin https://moveagency.com

run_decomposer install
[ "${status}" -eq 0 ]
[ "${lines[0]}" = "Installing Alpha...done" ]

assert_lib_installed Alpha-1.0 "${tag_alpha_lib_revision_hash}"
}

@test "${SUITE_NAME}: existing library fetches new annotated tag" {
create_decomposer_json alpha_tag_version

Expand Down

0 comments on commit 39d8810

Please sign in to comment.