From patchwork Mon Dec 28 19:23:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Contreras X-Patchwork-Id: 11991639 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,SPF_PASS,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 83A1CC433E6 for ; Mon, 28 Dec 2020 19:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5764322B2E for ; Mon, 28 Dec 2020 19:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728949AbgL1TXr (ORCPT ); Mon, 28 Dec 2020 14:23:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728791AbgL1TXr (ORCPT ); Mon, 28 Dec 2020 14:23:47 -0500 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 1CAC3C061793 for ; Mon, 28 Dec 2020 11:23:07 -0800 (PST) Received: by mail-oi1-x230.google.com with SMTP id s2so12457010oij.2 for ; Mon, 28 Dec 2020 11:23:07 -0800 (PST) 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=2O7fFwYzEC1uSzQvZ1Psa44We64HejPxHuCubWzgGvo=; b=FfYhMQ0PucAJEEXjgYBhz8WqxtieAh1OPhmiSQ6gikHP6ykKDCMu9BRzMwV5Ba2TWh mL6xqcAOUSrHaNDSMhR9dqEIen61uvWMeDllLpfGqq8wKHIG8+Qz0N2jdgpcV9pRSHA0 2QiVVPW1JoJWLjJ/NjRBFY3ZM41sXs44dh3pO+pVQ5qqognxSdM5s3jPJYERy6MNzI03 J/PHSFcSPdTNkhklho2DtCQxIcnjDlbPJ08sizNeaGOQjVn/rPl0JcKR3KnGmlM8THud f4bJy4JS+U2Hn/oZJR64RjKAqsk2NzRwXtef0qgOY+Z4sdwG3D6cEBu3SvQE8PRI3pyg iEJQ== 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=2O7fFwYzEC1uSzQvZ1Psa44We64HejPxHuCubWzgGvo=; b=iVoluqaLit4c1BFr9OKYFmqO8/XbynBDDGhm5ZtSWxm2awhbKfaxskAY4DIc9BWF/z QdQG6PCJkyJr4AoknFJPi614bViLY9kTXd0XuXhs2L9azGyXr+F8CaEHgDDdxkgoUUxi x6w0DqMO8SQ/ueieAhvbD96ub+A4LothLfkhQhtiADsgkGfUFItGyTsQwyeaZI+Ea+eP Up3v46swosdWpJDzFl/HO8AhmE+4mW8owXz5GOZwNoYWvMB/9FOJsUCpN8fr5pBhf0OV Q84efkGr3y1deboeWvEdXWqv9JqSAmGrcV0mP55bqLXv9E+eBRF/qsj7TkpfC3w9lcvU ffaw== X-Gm-Message-State: AOAM532iAiDIqICYfB+4IQEHdMQEECj9ZYUMxvZECZyca90Du+fCF4No zFqQr2nq5+H9Hxp0Xlbk+l7X895nC9mZ6g== X-Google-Smtp-Source: ABdhPJwUu/LIflWN4+QE3e4JgCmgofWDvtZXUfxhcYAiu7m/jR8ErcCWmhXhQpk7UMIEaanQBJexmA== X-Received: by 2002:aca:4c90:: with SMTP id z138mr268093oia.142.1609183386210; Mon, 28 Dec 2020 11:23:06 -0800 (PST) Received: from localhost (189-209-26-110.static.axtel.net. [189.209.26.110]) by smtp.gmail.com with ESMTPSA id 69sm8321384otc.76.2020.12.28.11.23.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Dec 2020 11:23:05 -0800 (PST) From: Felipe Contreras To: git@vger.kernel.org Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , Junio C Hamano , Jonathan Nieder , Felipe Contreras Subject: [PATCH 1/2] test: completion: add tests for __git_complete Date: Mon, 28 Dec 2020 13:23:01 -0600 Message-Id: <20201228192302.80467-2-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.30.0.rc2 In-Reply-To: <20201228192302.80467-1-felipe.contreras@gmail.com> References: <20201228192302.80467-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Felipe Contreras --- t/t9902-completion.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index a1c4f1f6d4..2e59fe4de0 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2380,4 +2380,11 @@ test_expect_success 'sourcing the completion script clears cached --options' ' verbose test -z "$__gitcomp_builtin_notes_edit" ' +test_expect_success '__git_complete' ' + __git_complete foo __git_main && + test "$(type -t __git_wrap__git_main)" == function && + __git_complete gf _git_fetch && + test "$(type -t __git_wrap_git_fetch)" == function +' + test_done From patchwork Mon Dec 28 19:23:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Contreras X-Patchwork-Id: 11991641 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,SPF_PASS,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 8DCC0C433DB for ; Mon, 28 Dec 2020 19:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C91D22B37 for ; Mon, 28 Dec 2020 19:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729082AbgL1TXu (ORCPT ); Mon, 28 Dec 2020 14:23:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728791AbgL1TXt (ORCPT ); Mon, 28 Dec 2020 14:23:49 -0500 Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06C0BC061796 for ; Mon, 28 Dec 2020 11:23:09 -0800 (PST) Received: by mail-ot1-x32f.google.com with SMTP id n42so9992118ota.12 for ; Mon, 28 Dec 2020 11:23:09 -0800 (PST) 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=FZVARGIqUCFVHLs76eJHmBQC6CmJf8wqjEC2IOZVttw=; b=czImC1ulf3oqSQxNAc2HVGeihoyXiV3dW0Xx4AdSOKDAIWHNCHK+MHMNlw2Fm5PCnK SE4gPxcfLxTQHOB2D/HKpyV7pwIiiLA8jOEbsTWLps3ZCE5eYHeYt2WqQMudXLjetZ+6 8gN1umwX/EPlVJFYYmNwdhtBovs4kBAqpu1frUshd5ODVjQVXeP4Honf9bkMIP5qUwjN BnJfpncXa7MustQom/SbixdL1/rLvhkWog+Zj/w4nUsZtqY2BnwGXMQHBT/aljjD8AWi 1/+VZeRH0kA6aJAGSJtSi0nfbJoneRNtUswMenVNhfbfaUtrXI45/AYw4ilEFU9hq9Ez Vt7w== 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=FZVARGIqUCFVHLs76eJHmBQC6CmJf8wqjEC2IOZVttw=; b=eGqXRzLYMuc/D5zoCFsWJ+tWxQ/4n0D7IzT7epjPnVOcm2PRJ8xNAq12MhuccB32gA rRvpuCICR0L3gOfWqLQSU5mVHu9AaMcbAL8JTXasrUMgv9RC7tD3fhkhD1U9Yd3ttOzR a0eqnIhZh4q7Ue1aftmGW5zu/d2yTwZbq2SyEL5zG5fnlBWTvwhzeZmwEcom5VjrxHI7 xil5n438kc6dKl2kN6vS4QdAx2YXqgxdSkLyjeLFD3GSRDgZdoqCEk3Sgzp5TY1I1tyW gYZtAGGaCejzAFlPRqgbFFu5vb9DR4sJe0QzJ/9tMBp7E1nC1tGm6Or5SVhBLuBbHfY2 cR1A== X-Gm-Message-State: AOAM531ZxWx1cBEliXN+L4xfg5hVAAoXtK/iSkHsnWK7W2EFnI6PYcUb 5Jhrs2eI0KvK+sh9WYmH73tCS417vR5Bag== X-Google-Smtp-Source: ABdhPJyEdSdC9zevqknG8j1G9w/adQJdtpaAqwgnrwObQvQOXMt7ltXH5qBfQMYN6UHKCmgIqTg4Hg== X-Received: by 2002:a05:6830:1de8:: with SMTP id b8mr33023931otj.204.1609183388098; Mon, 28 Dec 2020 11:23:08 -0800 (PST) Received: from localhost (189-209-26-110.static.axtel.net. [189.209.26.110]) by smtp.gmail.com with ESMTPSA id l12sm1410578ooq.22.2020.12.28.11.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Dec 2020 11:23:07 -0800 (PST) From: Felipe Contreras To: git@vger.kernel.org Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , Junio C Hamano , Jonathan Nieder , Felipe Contreras Subject: [PATCH 2/2] completion: add proper public __git_complete Date: Mon, 28 Dec 2020 13:23:02 -0600 Message-Id: <20201228192302.80467-3-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.30.0.rc2 In-Reply-To: <20201228192302.80467-1-felipe.contreras@gmail.com> References: <20201228192302.80467-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Back in 2012 I argued [1] for the introduction of a helper that would allow users to specify aliases like: git_complete gf git_fetch Back then there was pushback because there was no clear guideline for public functions (git_complete vs _git_complete vs _GIT_complete), and some aliases didn't actually work. Fast-forward to 2020 and there's still no guideline for public functions, and those aliases still don't work (even though I sent the fixes). This has not prevented people from using this function that is clearly needed to setup custom aliases [2], and in fact it's the recommended way. But it is cumbersome that the user must type: __git_complete gf _git_fetch Or worse: __git_complete gk __gitk_main 8 years is more than enough time to stop waiting for the perfect to come; let's define a public function (with the same name) that is actually user-friendly: __git_complete gf git_fetch __git_complete gk gitk While also maintaining backwards compatibility. The logic is: 1. If $2 exists, use it directly 2. If not, check if __$2_main exists 3. If not, check if _$2 exists 4. If not, fail [1] https://lore.kernel.org/git/1334524814-13581-1-git-send-email-felipe.contreras@gmail.com/ [2] https://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases Signed-off-by: Felipe Contreras --- contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------ t/t9902-completion.sh | 15 ++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 463a3124da..6b0432a72d 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3493,10 +3493,7 @@ __git_func_wrap () $1 } -# Setup completion for certain functions defined above by setting common -# variables and workarounds. -# This is NOT a public function; use at your own risk. -__git_complete () +___git_complete () { local wrapper="__git_wrap${2}" eval "$wrapper () { __git_func_wrap $2 ; }" @@ -3504,13 +3501,33 @@ __git_complete () || complete -o default -o nospace -F $wrapper $1 } -__git_complete git __git_main -__git_complete gitk __gitk_main +# Setup the completion for git commands +# 1: command or alias +# 2: function to call (e.g. `git`, `gitk`, `git_fetch`) +__git_complete () +{ + local func + + if [[ "$(type -t $2)" == function ]]; then + func=$2 + elif [[ "$(type -t __$2_main)" == function ]]; then + func=__$2_main + elif [[ "$(type -t _$2)" == function ]]; then + func=_$2 + else + echo "ERROR: could not find function '$2'" 1>&2 + return 1 + fi + ___git_complete $1 $func +} + +___git_complete git __git_main +___git_complete gitk __gitk_main # The following are necessary only for Cygwin, and only are needed # when the user has tab-completed the executable name and consequently # included the '.exe' suffix. # if [ "$OSTYPE" = cygwin ]; then - __git_complete git.exe __git_main + ___git_complete git.exe __git_main fi diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 2e59fe4de0..53669a36f1 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2381,10 +2381,23 @@ test_expect_success 'sourcing the completion script clears cached --options' ' ' test_expect_success '__git_complete' ' + unset -f __git_wrap__git_main && + __git_complete foo __git_main && test "$(type -t __git_wrap__git_main)" == function && + unset -f __git_wrap__git_main && + __git_complete gf _git_fetch && - test "$(type -t __git_wrap_git_fetch)" == function + test "$(type -t __git_wrap_git_fetch)" == function && + + __git_complete foo git && + test "$(type -t __git_wrap__git_main)" == function && + unset -f __git_wrap__git_main && + + __git_complete gd git_diff && + test "$(type -t __git_wrap_git_diff)" == function && + + test_must_fail __git_complete ga missing ' test_done