Skip to content
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

perf: optimize update_plugin function #1665

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/functions/plugins.bash
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ plugin_update_command() {
update_plugin() {
local plugin_name=$1
local plugin_path=$2
plugin_remote_default_branch=$(git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }')
local gitref=${3:-${plugin_remote_default_branch}}
local common_git_options=(--git-dir "$plugin_path/.git" --work-tree "$plugin_path")
local gitref=${3:-$(git "${common_git_options[@]}" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }')}
logfile=$(mktemp)

local common_git_options=(--git-dir "$plugin_path/.git" --work-tree "$plugin_path")
local prev_ref=
local post_ref=
{
Expand All @@ -146,7 +145,17 @@ update_plugin() {

printf "Updating %s to %s\n" "$plugin_name" "$gitref"

git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref"
local is_commit=false
rp=$(git "${common_git_options[@]}" rev-parse --verify -q "$gitref" || true)
# if rp is non-empty and gitref is a prefix of rp, then gitref is a commit
if [[ -n "$rp" && "$rp" == "$gitref"* ]]; then
is_commit=true
fi

# if gitref is not commit, or it is a commit but it's not present in the already cloned repo, then fetch from remote
if ! $is_commit || ! git "${common_git_options[@]}" cat-file -t "$gitref" >/dev/null 2>&1; then
git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref"
fi
prev_ref=$(git "${common_git_options[@]}" rev-parse --short HEAD)
post_ref=$(git "${common_git_options[@]}" rev-parse --short "${gitref}")
git "${common_git_options[@]}" -c advice.detachedHead=false checkout --force "$gitref"
Expand Down