diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash index 6f7a9435c..3ee912653 100644 --- a/lib/functions/plugins.bash +++ b/lib/functions/plugins.bash @@ -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= { @@ -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"