From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21918 invoked from network); 29 Oct 2020 01:28:55 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 29 Oct 2020 01:28:55 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1603934935; b=nq/IfKGiEE3JKlq0ftXjghdsvhJVkWRhWD5LHh5UoJQN6qJ3qcvy5w9R5OFSrxAdnXRgrVx5nh vDLCr59Brf4WpjRGiDPO1kxt6j2vNa4R99Src34g+W7GUgJu4cOL3piixapmC/LXYt1qBUFn72 k63O8spiAWo7kFCEG5R4PEffI9Dhjo6227qaSu7TnnIRjWhA2NuZyF6AniDdldXMNyrl7z1wxg VY2e8fs4MuOyFn5CI880WayvxUnA+U3832uysV23bCYhgJj2sLQN4kstQv5kCSl3IbJlw3GPrE AroN+QMFchwtBxYfhzPYMWCqlYjh72plkA/2LoowoGJSOw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-lj1-f193.google.com) smtp.remote-ip=209.85.208.193; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1603934935; bh=K0eW1OZqKI7/EIqmj4k2cuBeeA5wYE0KY1aqZjmFVLk=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:MIME-Version:Message-ID:Date: Subject:Cc:To:From:DKIM-Signature:DKIM-Signature; b=JMSYHdYmI6MdCZM6Z+nuz5gM9ftouWBll9+Q9LrySlzVbsWNvZ3ZdB5kzcQThHD9gSyf1I5wz+ PewW+2XXt4zab7vMElCaDKtmoWKKdjIPD0w94Si6oGgtLNZM8JW7VHiNx/k8zH2dOd2+HjSPfD JDffJPqSzDHdPgWA6iEVaHXMXYQbB+T9z+ROHug0uV3+JBQHV9XSQ56neRDIM7V3whN+qItDf1 jl5TqR6Bd5Kmd2xu1cR4UCAWzHW2m9vaFfv8hd3kJYm5mI8hm8KvfMZVza4vSK04V/Zar6IJE7 alABEiQ9EX08SlWJ5VzQ2XpIeeIPWEjYVXawPOUmzWOJYg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References; bh=lMuTJaKLWxLj+4jkKaLrzwBhVMwwu9aSFiJK5fggqQM=; b=hN9zexPOyyUn5HVWOTjGgDug/h kZkVZtqFuhrluPLo61F6KjJopDkerl0q/0yNJ0IEPbRCNDvHmlA13nFTJpY3iwfuDRtWjtR5jVU3Y VdCYeHWh6kYRj4oaqwIpXMUWORxsUmzyLYwpP+kjV/bAHGGCALq3OtFL0RqPHKiUKrZHRUngQoxRL Q5GdMV7IXVgwUCv7t/4awJsKsaL5u5ZvwxFh9hIUrW6nMIDwXtwlhQYj4VLAvvantfdViwou7rNMr BB57RrBYbQNcaAR8vi6C/MwVgwZ6sS4FQ6Ug/LlMQLj2ZB8gCuf/IxdPly55j4bXi8cLry3DVoD5B gU0m+jSQ==; Received: from authenticated user by zero.zsh.org with local id 1kXwkB-000Jxb-RS; Thu, 29 Oct 2020 01:28:47 +0000 Authentication-Results: zsh.org; iprev=pass (mail-lj1-f193.google.com) smtp.remote-ip=209.85.208.193; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-lj1-f193.google.com ([209.85.208.193]:33746) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1kXwjp-000JnK-MH; Thu, 29 Oct 2020 01:28:26 +0000 Received: by mail-lj1-f193.google.com with SMTP id c21so1363250ljj.0 for ; Wed, 28 Oct 2020 18:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lMuTJaKLWxLj+4jkKaLrzwBhVMwwu9aSFiJK5fggqQM=; b=poO0YiYrOXHYmMCGLL8c9IkMP5akAnM1etZAnmnDfhzC7M+i1zeG1XCceXce1xSih9 PIemYmW8Z+Z7BDR3pRbUHBpaIl7Wdhv48JG4Q6TTDT1+uQDqOFy+2G6tFPqocluCcK07 gASfqGg6ZBhIxJWxRoapKvlCXjYzmyjXgLcr6dzyhi5/jWo3DKWvVczcLUrqPyCh+5++ BoCerih2aEk1NOR+4JOevUrlwWJljrrrs0Hz5v69gOv7R2aqqQclTtF4N3QCQ6n//slo o17kf4JBFm1iKSSYTX9iLfhU0j7W0TD764AxhGTV127xhlbwPX+rFPEpYHL2RbBAy4Zz JxMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lMuTJaKLWxLj+4jkKaLrzwBhVMwwu9aSFiJK5fggqQM=; b=qNJOOhUd9X0FlWBS6gm8SpDXUIXF8iEIC0Ie1upSrn6KIsa+NKcCmbYmx1g7uAZBJ5 BPNVLE48qjnAckEtpM1wAy7/iZerV+ajsZcsm7PitvdboA2yBOBpD1FUz6oTU8vapkB1 pDU9EDDHM/h3rkFgf6vUR8wlIhIUWL9qET4tokeyhtr6LdjPUrOptBFTJsv8jHty0rfm t7hW2ozR0GO+Z4gk/4N5U7zVgw76/gRPFSLBKe/ajXVopCtIhC2PB5oTND9u7BF1wT0z gB6bmwB1FFMoMyeM1d78XBRycyrkbDWy6kwicskLooEEr00x6CppLSj5pbTkyIkgIk4I 3HPA== X-Gm-Message-State: AOAM532AgJcaqfdcMM7JYH6oJDZ4ZfJxKBWkyRQ8blSDZda6FgN1Xp4J Of3HRymGU8ZHDDjTMbkqdNuSuyW+Hw/7mSNl X-Google-Smtp-Source: ABdhPJymJJZyq2bvtNWO93H4ZaLGy2J8lxgy2NE20cK+xwJPKh3/OVbpMnWxfVX44PHb97G7W60DQQ== X-Received: by 2002:a2e:a174:: with SMTP id u20mr829468ljl.101.1603934904362; Wed, 28 Oct 2020 18:28:24 -0700 (PDT) Received: from trx40.lan ([2a02:2698:542a:a7e::500]) by smtp.gmail.com with ESMTPSA id y27sm143917ljm.74.2020.10.28.18.28.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2020 18:28:23 -0700 (PDT) From: Aleksandr Mezin To: zsh-workers@zsh.org Cc: Aleksandr Mezin Subject: [RFC PATCH v3] vcs_info: choose backend by basedir Date: Thu, 29 Oct 2020 07:27:55 +0600 Message-Id: <20201029012755.253085-1-mezin.alexander@gmail.com> X-Mailer: git-send-email 2.29.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Seq: 47508 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: Previously, vcs_info iterated over enabled VCS backends and output repository status from the first backend that works (i.e. first backend that can find a repository). But I prefer a different behavior: I want to see the status of the repository closest to the current working directory. For example: if there is a Mercurial repository inside a Git repository, and I 'cd' into the Mercurial repository, I want to see the status of Mercurial repo, even if 'git' comes before 'hg' in the 'enable' list. This patch changes how vcs_info chooses the backend: 1. Among backends whose basedir isn't an ancestor of the current directory, old algorithm is used: whichever comes first in 'enable' list. 2. If all enabled backends return basedirs that are ancestors of the current directory, the one with basedir closest to the current directory is chosen. As far as I know, unless the user has set basedir manually (GIT_WORK_TREE), all backend return basedirs that are ancestors of the current directory. Backends that return non-ancestor basedirs have higher priority to show the status of that manually-configured repository. It's possible to revert to the old behavior of vcs_info, by setting: zstyle ':vcs_info:*' backend-choice enablement-order It can also be set per-backend: zstyle ':vcs_info:hg:*' backend-choice enablement-order This effectively moves the backend into group (1), as if its basedir is never an ancestor of the current directory. Not sure how useful this is though. Also, this patch adjusts VCS_INFO_detect_git and VCS_INFO_detect_cvs to make them set vcs_comm[basedir], by moving basedir detection code from corresponding VCS_INFO_get_data_* functions. VCS_INFO_detect_git and VCS_INFO_detect_cvs were the only VCS_INFO_detect_* functions not setting it. I've omitted the documentation from this patch. If the code (and the approcah) here is ok, I'll work on the documentation later. Signed-off-by: Aleksandr Mezin --- .../VCS_Info/Backends/VCS_INFO_detect_cvs | 17 +++++++++-- .../VCS_Info/Backends/VCS_INFO_detect_git | 1 + .../VCS_Info/Backends/VCS_INFO_get_data_cvs | 11 +------ .../VCS_Info/Backends/VCS_INFO_get_data_git | 2 +- Functions/VCS_Info/vcs_info | 30 ++++++++++++++++--- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Functions/VCS_Info/Backends/VCS_INFO_detect_cvs b/Functions/VCS_Info/Backends/VCS_INFO_detect_cvs index 7a5ee1eef..a57959ec9 100644 --- a/Functions/VCS_Info/Backends/VCS_INFO_detect_cvs +++ b/Functions/VCS_Info/Backends/VCS_INFO_detect_cvs @@ -7,5 +7,18 @@ setopt localoptions NO_shwordsplit [[ $1 == '--flavours' ]] && return 1 VCS_INFO_check_com ${vcs_comm[cmd]} || return 1 -[[ -d "./CVS" ]] && [[ -r "./CVS/Repository" ]] && return 0 -return 1 +if ! [[ -d "./CVS" ]] || ! [[ -r "./CVS/Repository" ]] ; then + return 1 +fi + +# Look for the most distant parent that still has a CVS subdirectory. +local cvsbase="." +cvsbase=${cvsbase:P} +while [[ -d "${cvsbase:h}/CVS" ]]; do + cvsbase="${cvsbase:h}" + if [[ $cvsbase == '/' ]]; then + break + fi +done + +vcs_comm[basedir]="${cvsbase}" diff --git a/Functions/VCS_Info/Backends/VCS_INFO_detect_git b/Functions/VCS_Info/Backends/VCS_INFO_detect_git index e4191f474..b7955de38 100644 --- a/Functions/VCS_Info/Backends/VCS_INFO_detect_git +++ b/Functions/VCS_Info/Backends/VCS_INFO_detect_git @@ -7,6 +7,7 @@ setopt localoptions NO_shwordsplit [[ $1 == '--flavours' ]] && { print -l git-p4 git-svn; return 0 } if VCS_INFO_check_com ${vcs_comm[cmd]} && vcs_comm[gitdir]="$(${vcs_comm[cmd]} rev-parse --git-dir 2> /dev/null)" ; then + vcs_comm[basedir]="$( ${vcs_comm[cmd]} rev-parse --show-toplevel 2> /dev/null )" if [[ -d ${vcs_comm[gitdir]}/svn ]] ; then vcs_comm[overwrite_name]='git-svn' elif [[ -d ${vcs_comm[gitdir]}/refs/remotes/p4 ]] ; then vcs_comm[overwrite_name]='git-p4' ; fi return 0 diff --git a/Functions/VCS_Info/Backends/VCS_INFO_get_data_cvs b/Functions/VCS_Info/Backends/VCS_INFO_get_data_cvs index 9b828bd11..bc0d5cfe5 100644 --- a/Functions/VCS_Info/Backends/VCS_INFO_get_data_cvs +++ b/Functions/VCS_Info/Backends/VCS_INFO_get_data_cvs @@ -5,17 +5,8 @@ setopt localoptions NO_shwordsplit local cvsbranch cvsbase -# Look for the most distant parent that still has a CVS subdirectory. +cvsbase="${vcs_comm[basedir]}" # VCS_INFO_detect_cvs ensured that ./CVS/Repository exists. -cvsbase="." -cvsbase=${cvsbase:P} -while [[ -d "${cvsbase:h}/CVS" ]]; do - cvsbase="${cvsbase:h}" - if [[ $cvsbase == '/' ]]; then - break - fi -done - cvsbranch=$(< ./CVS/Repository) rrn=${cvsbase:t} cvsbranch=${cvsbranch##${rrn}/} diff --git a/Functions/VCS_Info/Backends/VCS_INFO_get_data_git b/Functions/VCS_Info/Backends/VCS_INFO_get_data_git index 79429c8e0..eb04d4b41 100644 --- a/Functions/VCS_Info/Backends/VCS_INFO_get_data_git +++ b/Functions/VCS_Info/Backends/VCS_INFO_get_data_git @@ -138,7 +138,7 @@ VCS_INFO_git_handle_patches () { gitdir=${vcs_comm[gitdir]} VCS_INFO_git_getbranch ${gitdir} -gitbase=$( ${vcs_comm[cmd]} rev-parse --show-toplevel 2> /dev/null ) +gitbase=${vcs_comm[basedir]} if [[ -z ${gitbase} ]]; then # Bare repository gitbase=${gitdir:P} diff --git a/Functions/VCS_Info/vcs_info b/Functions/VCS_Info/vcs_info index 786b61918..de8efc6b4 100644 --- a/Functions/VCS_Info/vcs_info +++ b/Functions/VCS_Info/vcs_info @@ -54,7 +54,7 @@ vcs_info () { [[ -r . ]] || return 0 local pat - local -i found retval + local -i retval local -a enabled disabled dps local usercontext vcs rrn quiltmode local -x LC_MESSAGES @@ -114,7 +114,9 @@ vcs_info () { VCS_INFO_maxexports - (( found = 0 )) + local -A chosen_vcs_comm chosen_backend_misc + local chosen_vcs basedir_realpath chosen_basedir_realpath choose_closest + for vcs in ${enabled} ; do [[ -n ${(M)disabled:#${vcs}} ]] && continue if (( ${+functions[VCS_INFO_detect_${vcs}]} == 0 )) ; then @@ -124,10 +126,30 @@ vcs_info () { fi vcs_comm=() VCS_INFO_get_cmd - VCS_INFO_detect_${vcs} && (( found = 1 )) && break + if VCS_INFO_detect_${vcs}; then + basedir_realpath="${vcs_comm[basedir]:P}" + + zstyle -m ":vcs_info:${vcs}:${usercontext}:${rrn}" backend-choice enablement-order + choose_closest=$? + + [[ "${PWD:P}/" != "${basedir_realpath}"/* ]] && choose_closest=0 + + if (( ! choose_closest )) || (( ${#basedir_realpath} > ${#chosen_basedir_realpath} )) ; then + chosen_vcs="${vcs}" + chosen_vcs_comm=("${(kv)vcs_comm[@]}") + chosen_backend_misc=("${(kv)backend_misc[@]}") + chosen_basedir_realpath="${basedir_realpath}" + + (( ! choose_closest )) && break + fi + fi done - (( found == 0 )) && { + vcs="${chosen_vcs}" + vcs_comm=("${(kv)chosen_vcs_comm[@]}") + backend_misc=("${(kv)chosen_backend_misc[@]}") + + [ -z "${vcs}" ] && { vcs='-quilt-'; quiltmode='standalone' VCS_INFO_quilt standalone || VCS_INFO_set --nvcs return 0 -- 2.29.1