From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2630C433B4 for ; Mon, 26 Apr 2021 16:20:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9A68E61164 for ; Mon, 26 Apr 2021 16:20:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234523AbhDZQVe (ORCPT ); Mon, 26 Apr 2021 12:21:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233919AbhDZQVd (ORCPT ); Mon, 26 Apr 2021 12:21:33 -0400 Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E981DC061574 for ; Mon, 26 Apr 2021 09:20:51 -0700 (PDT) Received: by mail-oi1-x230.google.com with SMTP id k25so56978270oic.4 for ; Mon, 26 Apr 2021 09:20:51 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=O7RW+8hJP0Nay3XXmOTYHw106DOqFYPtV5o2FLwxBgE=; b=OVXEGH7dtBucvMccIrxXKudSgTKefwLkVoqXI/Lw/f4c3HhMq6we5rUTT8iPE85Zzt /l4W1FkpNmIDJCePC5p5pVwrACmKWxh+0urlqnYZV34IGeoUy8BmN9s0v+F/b9iF6x6y 1cwcCUFknwCra/6F/PTaruDVJFIALdV9JDQKBjpXsKqSs4GB4IryoQw/f3Y46OVPduS8 LS6owkcZ3mwisbVIi6t7TMeipVjImFq9nZnsJdI6dSXCml6U209t34hbMKWg6PttjVbb Xf0qZOTtAWZTb+GPxPThgsuFUIFVFAUSZ8kDg/stA2F7kAdWzXjARVO8ekHRSeyZdxu8 l/rg== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=O7RW+8hJP0Nay3XXmOTYHw106DOqFYPtV5o2FLwxBgE=; b=oCwwA3coSSsodqycM4iP5v+f8wZY1AIm0rydUtf08dO/HwCrkL3sSb+97JQ6payOf3 6+muZaQIKJ11CsYx+geUxfV0EcnNPB1cSiQxQtX/MWB9W0rf5/bgQ7++5m6VeYQUv6KJ 7A8tShHZG40OG1AeJPzZEincm23+Fd4LSw8Eg2eAtKEcjiQKMjJctUJ4u2W8fuSr508o ufdiQIA6JNoIFQjKkuWyDzVyEnmPqg17xRcncCcUnzqPhE5PKmV8HBg7t5Oe7PA6tQKX j0nPtAhEfZIdNxMtF2GzsIda9yc18BE3+zbkLTe9g4cmwdo7PvJg5kHqawOPvCCupLTd ILIA== X-Gm-Message-State: AOAM533SzIcQmMXXuByo/BdPQ0wOuo2tn2ZoppUZVwbn9XVbwjViVQbc qgRA24LMGngs0KkZUpJCA1LYwv+rLdIf+g== X-Google-Smtp-Source: ABdhPJxllk45qSiaEiyKi9BzZnc4W1/H31dgYgQ3xcpaKZI8zkqg8QNoyWTFhb53tob5esjPr1lb4Q== X-Received: by 2002:a05:6808:699:: with SMTP id k25mr13638527oig.101.1619454051060; Mon, 26 Apr 2021 09:20:51 -0700 (PDT) Received: from localhost (fixed-187-190-78-172.totalplay.net. [187.190.78.172]) by smtp.gmail.com with ESMTPSA id 10sm3088665oiq.48.2021.04.26.09.20.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Apr 2021 09:20:50 -0700 (PDT) From: Felipe Contreras To: git@vger.kernel.org Cc: Felipe Contreras Subject: [PATCH v1 39/43] completion: bash: refactor _get_comp_words_by_ref() Date: Mon, 26 Apr 2021 11:14:54 -0500 Message-Id: <20210426161458.49860-40-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210426161458.49860-1-felipe.contreras@gmail.com> References: <20210426161458.49860-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We don't need a separate function to do what we already know we want to do. Signed-off-by: Felipe Contreras --- contrib/completion/git-completion.bash | 93 +++++++++++--------------- 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index cbea35441c..05b6762210 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -334,21 +334,11 @@ __git_dequote () # # RELEASE: 2.x -# This function can be used to access a tokenized list of words -# on the command line: -# -# __git_reassemble_comp_words_by_ref '=:' -# if test "${words_[cword_-1]}" = -w -# then -# ... -# fi -# -# The argument should be a collection of characters from the list of -# word completion separators (COMP_WORDBREAKS) to treat as ordinary -# characters. +# This function reorganizes the words on the command line to be processed by +# the rest of the script. # # This is roughly equivalent to going back in time and setting -# COMP_WORDBREAKS to exclude those characters. The intent is to +# COMP_WORDBREAKS to exclude '=' and ':'. The intent is to # make option types like --date= and : easy to # recognize by treating each shell word as a single token. # @@ -356,60 +346,55 @@ __git_dequote () # shared with other completion scripts. By the time the completion # function gets called, COMP_WORDS has already been populated so local # changes to COMP_WORDBREAKS have no effect. -# -# Output: words_, cword_, cur_. -__git_reassemble_comp_words_by_ref() +if ! type _get_comp_words_by_ref >/dev/null 2>&1; then +_get_comp_words_by_ref () { + local words_ cword_ local exclude i j first + # Which word separators to exclude? - exclude="${1//[^$COMP_WORDBREAKS]}" + exclude="${COMP_WORDBREAKS//[^=:]}" cword_=$COMP_CWORD if [ -z "$exclude" ]; then words_=("${COMP_WORDS[@]}") - return - fi - # List of word completion separators has shrunk; - # re-assemble words to complete. - for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do - # Append each nonempty word consisting of just - # word separator characters to the current word. - first=t - while - [ $i -gt 0 ] && - [ -n "${COMP_WORDS[$i]}" ] && - # word consists of excluded word separators - [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ] - do - # Attach to the previous token, - # unless the previous token is the command name. - if [ $j -ge 2 ] && [ -n "$first" ]; then - ((j--)) - fi - first= + else + # List of word completion separators has shrunk; + # re-assemble words to complete. + for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do + # Append each nonempty word consisting of just + # word separator characters to the current word. + first=t + while + [ $i -gt 0 ] && + [ -n "${COMP_WORDS[$i]}" ] && + # word consists of excluded word separators + [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ] + do + # Attach to the previous token, + # unless the previous token is the command name. + if [ $j -ge 2 ] && [ -n "$first" ]; then + ((j--)) + fi + first= + words_[$j]=${words_[j]}${COMP_WORDS[i]} + if [ $i = $COMP_CWORD ]; then + cword_=$j + fi + if (($i < ${#COMP_WORDS[@]} - 1)); then + ((i++)) + else + # Done. + break 2 + fi + done words_[$j]=${words_[j]}${COMP_WORDS[i]} if [ $i = $COMP_CWORD ]; then cword_=$j fi - if (($i < ${#COMP_WORDS[@]} - 1)); then - ((i++)) - else - # Done. - return - fi done - words_[$j]=${words_[j]}${COMP_WORDS[i]} - if [ $i = $COMP_CWORD ]; then - cword_=$j - fi - done -} + fi -if ! type _get_comp_words_by_ref >/dev/null 2>&1; then -_get_comp_words_by_ref () -{ - local words_ cword_ - __git_reassemble_comp_words_by_ref "=:" cword=$cword_ cur=${words_[cword]} prev=${words_[cword-1]} -- 2.31.0