Skip to content

Commit

Permalink
Avoiding excessive cd when fetching git repos (#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav authored Oct 16, 2023
1 parent 7a741cf commit aada31a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
44 changes: 17 additions & 27 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -494,37 +494,27 @@ fetch_git() {

echo "Cloning ${git_url}..." >&2

if type git &>/dev/null; then
if [ -n "$RUBY_BUILD_CACHE_PATH" ]; then
# shellcheck disable=SC2164
pushd "$RUBY_BUILD_CACHE_PATH" >&4
local clone_name
clone_name="$(sanitize "$git_url")"
if [ -e "$clone_name" ]; then
{ # shellcheck disable=SC2164
cd "$clone_name"
git fetch --force "$git_url" "+${git_ref}:${git_ref}"
} >&4 2>&1
else
git clone --bare --branch "$git_ref" "$git_url" "${clone_name}" >&4 2>&1
fi
git_url="$RUBY_BUILD_CACHE_PATH/${clone_name}"
# shellcheck disable=SC2164
popd >&4
fi
if ! type git &>/dev/null; then
echo "error: please install \`git\` and try again" >&2
exit 1
fi

if [ -e "${package_name}" ]; then
( # shellcheck disable=SC2164
cd "${package_name}"
git fetch --depth 1 origin "+${git_ref}"
git checkout -q -B "$git_ref" "origin/${git_ref}"
) >&4 2>&1
if [ -n "$RUBY_BUILD_CACHE_PATH" ]; then
local cache_dir
cache_dir="$RUBY_BUILD_CACHE_PATH/$(sanitize "$git_url")"
if [ -e "$cache_dir" ]; then
git -C "$cache_dir" fetch --force "$git_url" "+${git_ref}:${git_ref}" >&4 2>&1
else
git clone --depth 1 --branch "$git_ref" "$git_url" "${package_name}" >&4 2>&1
git clone --bare --branch "$git_ref" "$git_url" "$cache_dir" >&4 2>&1
fi
git_url="$cache_dir"
fi

if [ -e "${package_name}" ]; then
git -C "$package_name" fetch --depth 1 origin "+${git_ref}" >&4 2>&1
git -C "$package_name" checkout -q -B "$git_ref" "origin/${git_ref}" >&4 2>&1
else
echo "error: please install \`git\` and try again" >&2
exit 1
git clone --depth 1 --branch "$git_ref" "$git_url" "${package_name}" >&4 2>&1
fi
}

Expand Down
4 changes: 2 additions & 2 deletions test/fetch.bats
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ OUT
@test "updating existing git repository" {
mkdir -p "${RUBY_BUILD_BUILD_PATH}/package-dev"
stub git \
"fetch --depth 1 origin +master : true" \
"checkout -q -B master origin/master : true"
"-C package-dev fetch --depth 1 origin +master : true" \
"-C package-dev checkout -q -B master origin/master : true"

run_inline_definition <<DEF
install_git "package-dev" "http://example.com/packages/package.git" master copy
Expand Down

0 comments on commit aada31a

Please sign in to comment.