From 395e9dbdf0e70d2d4ded9dbce87c99a638331a2a Mon Sep 17 00:00:00 2001 From: Giorgio Garasto Date: Wed, 11 Oct 2023 08:58:22 +0200 Subject: [PATCH] perf: only fetch updated from remote repo if needed --- lib/functions/plugins.bash | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash index 05496ad09..5ae89b603 100644 --- a/lib/functions/plugins.bash +++ b/lib/functions/plugins.bash @@ -145,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"