From a42def5c6d4765258f9a79c24042913ff7289a15 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 30 Mar 2017 06:08:55 -0700 Subject: [PATCH] tg: awksome accelerate top-bases computation Make use of the new run_awk_ref_prefixes function to compute the active top-bases prefix for the repository or the remote being populated. Although only performed once at tg startup (or remote --populate time), using the awksome function makes the overhead for the computation practically nil. It also brings the side benefit of ignoring orphan bases (bases with no corresponding head) to the remote --populate operation. A win all around. Signed-off-by: Kyle J. McKay --- tg-remote.sh | 24 ++++++------------ tg.sh | 82 ++++++++++++++++++------------------------------------------ 2 files changed, 33 insertions(+), 73 deletions(-) diff --git a/tg-remote.sh b/tg-remote.sh index 9736cfd..9e410e6 100644 --- a/tg-remote.sh +++ b/tg-remote.sh @@ -41,20 +41,12 @@ if [ -n "$topbases_implicit_default" ]; then "+refs/heads/*:refs/remotes/$name/*" fi # see if we have any remote bases - sawnew= - sawold= - while read -r rn && [ -n "$rn" ]; do - case "$rn" in - "refs/remotes/$name/{top-bases}"/?*) - sawnew=1;; - "refs/remotes/$name/top-bases"/?*) - sawold=1;; - esac - [ "$sawnew$sawold" != "11" ] || break - done <<-EOT - $(git for-each-ref --format='%(refname)' "refs/remotes/$name/{top-bases}" "refs/remotes/$name/top-bases") - EOT - if [ "$sawold$sawnew" = "11" ]; then + rc=0 remotebases= + remotebases="$( + git for-each-ref --format='%(refname)' "refs/remotes/$name" 2>/dev/null | + run_awk_ref_prefixes -n -- "refs/remotes/$name/{top-bases}" "refs/remotes/$name/top-bases" "refs/remotes/$name")" || + rc=$? + if [ "$rc" = "65" ]; then err "remote \"$name\" has top-bases in both locations:" err " refs/remotes/$name/{top-bases}/..." err " refs/remotes/$name/top-bases/..." @@ -65,9 +57,9 @@ if [ -n "$topbases_implicit_default" ]; then err "(the tg migrate-bases command can also help with this problem)" die "schizophrenic remote \"$name\" requires topgit.top-bases setting" fi - if [ -n "$sawold$sawnew" ]; then + if [ -n "$remotebases" ]; then val="heads" - [ -z "$sawold" ] || val="refs" + [ "$remotebases" = "refs/remotes/$name/{top-bases}" ] || val="refs" GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'topgit.top-bases=$val'" export GIT_CONFIG_PARAMETERS unset tg_topbases_set diff --git a/tg.sh b/tg.sh index 5164245..18c433d 100644 --- a/tg.sh +++ b/tg.sh @@ -1887,68 +1887,36 @@ set_topbases() # check heads and top-bases and see what state the current # repository is in. remotes are ignored. - hblist=" " - topbases= - both= - newtb="heads/{top-bases}" - while read -r rn && [ -n "$rn" ]; do case "$rn" in - "refs/heads/{top-bases}"/*) - case "$hblist" in *" ${rn#refs/$newtb/} "*) - if [ "$topbases" != "heads/{top-bases}" ] && [ -n "$topbases" ]; then - both=1 - break; - else - topbases="heads/{top-bases}" - topbasesrx="heads/[{]top-bases[}]" - oldbases="top-bases" - fi - esac;; - "refs/top-bases"/*) - case "$hblist" in *" ${rn#refs/top-bases/} "*) - if [ "$topbases" != "top-bases" ] && [ -n "$topbases" ]; then - both=1 - break; - else - topbases="top-bases" - topbasesrx="top-bases" - oldbases="heads/{top-bases}" - fi - esac;; - "refs/heads"/*) - hblist="$hblist${rn#refs/heads/} ";; - esac; done <<-EOT - $(git for-each-ref --format='%(refname)' "refs/heads" "refs/top-bases" 2>/dev/null) - EOT - if [ -n "$both" ]; then - if [ -n "$1" ]; then - # hook script always prefers newer without complaint - topbases="heads/{top-bases}" - topbasesrx="heads/[{]top-bases[}]" - oldbases="top-bases" - else - # Complain and die - err "repository contains existing TopGit branches" - err "but some use refs/top-bases/... for the base" - err "and some use refs/heads/{top-bases}/... for the base" - err "with the latter being the new, preferred location" - err "set \"topgit.top-bases\" to either \"heads\" to use" - err "the new heads/{top-bases} location or \"refs\" to use" - err "the old top-bases location." - err "(the tg migrate-bases command can also resolve this issue)" - die "schizophrenic repository requires topgit.top-bases setting" - fi - elif [ -n "$topbases" ]; then - unset topbases_implicit_default - fi - - [ -n "$topbases" ] || { + rc=0 activebases= + activebases="$( + git for-each-ref --format='%(refname)' "refs/heads" "refs/top-bases" 2>/dev/null | + run_awk_ref_prefixes ${1:+-e} -n -- "refs/heads/{top-bases}" "refs/top-bases" "refs/heads")" || + rc=$? + if [ "$rc" = "65" ]; then + # Complain and die + err "repository contains existing TopGit branches" + err "but some use refs/top-bases/... for the base" + err "and some use refs/heads/{top-bases}/... for the base" + err "with the latter being the new, preferred location" + err "set \"topgit.top-bases\" to either \"heads\" to use" + err "the new heads/{top-bases} location or \"refs\" to use" + err "the old top-bases location." + err "(the tg migrate-bases command can also resolve this issue)" + die "schizophrenic repository requires topgit.top-bases setting" + fi + [ -z "$activebases" ] || unset topbases_implicit_default + if [ "$activebases" = "refs/heads/{top-bases}" ]; then + topbases="heads/{top-bases}" + topbasesrx="heads/[{]top-bases[}]" + oldbases="top-bases" + else # default is still top-bases for now topbases="top-bases" topbasesrx="top-bases" oldbases="heads/{top-bases}" - } + fi # MUST NOT be exported - unset hblist both newtb rn tg_topases_set + unset rc activebases tg_topases_set tg_topbases_set=1 return 0 } -- 2.11.4.GIT