From patchwork Tue Dec 29 17:08:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Contreras X-Patchwork-Id: 11992515 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.8 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 72D2EC433E0 for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CE0D20867 for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726185AbgL2RJX (ORCPT ); Tue, 29 Dec 2020 12:09:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgL2RJW (ORCPT ); Tue, 29 Dec 2020 12:09:22 -0500 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DB68C0613D6 for ; Tue, 29 Dec 2020 09:08:42 -0800 (PST) Received: by mail-oi1-x229.google.com with SMTP id d203so15242749oia.0 for ; Tue, 29 Dec 2020 09:08:42 -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=LYMFb60oj1ozuA3IBJgnIIJ4JjKCmOzQUHPrcHLsidA=; b=tMP838FRz5wBmz4nfZYf1KKQG/Ms4k7Os90iF0TgLWNHz1za6y/NjDhgc0tufzHydh XTt5V9g6BQtobeyjd0Wd6ps7b9HY2HhL45ogjgTYIlYj5FJQ4/ou6Wodrpx67RYVvySQ fT7/i67u49mkVBtKLQsG009d4Slj7hbqOYgiq6Isr6Boeu3Z2HJfhrEi8t1/yqSTmvrr ytNScXHw9ivhRjm+E5CSVmGcywisEv6xaoyUtbkkCdCV4AqIcJmCHg18lXAZO1MBKBiF Z9A9y4+PLwzt6pnSlJnjVtxlWo+yanDfPDqkKLQLSrY/wIBhYfYkhyFm7hG0xFar8mAl KQSA== 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=LYMFb60oj1ozuA3IBJgnIIJ4JjKCmOzQUHPrcHLsidA=; b=R0uWdzAxgQeNCPIo/mtS3CLZyKe6k8S2X+BQww6c/3zoOdJChU7bjm1sa2IjDn0Tud zLdFpfk4LZ9XMCmacxvEha9hWlcl8yjt83AZ9r9+BlIPaXMGf6mOUJqoK2bph/BdlO0J 6Q16kGvP04Q92kyDzd5pSYfiPsOCoan5cx1fyiBAYb0Ok1YJ/OUW82Zy7sB1OxVDtUSx YeNMiQIoa0eLVno6olFmmk0t5bMfwFY2bpxhAD5S58EItrMUDSwiy+h4AU6a+zKwkiBd iI6dJzpVjEAa3mGT0ZqOVh5At4ohBSjFgvZNyRh92nzUuYiyewe7SCDG8i4KP1zXV9ym CeEQ== X-Gm-Message-State: AOAM530sbE7dgR0MWl5EXgN4upEhwImjYOn6KuEmnNWhYCllvnQLTjyA Nz3vaxswugXqHCjDwG9+3+fJ3C+N/XvyhQ== X-Google-Smtp-Source: ABdhPJzpVAJGjKmr2UIVMn/Psf5RWRLuESw1w7d3umVHPq7dD5x7squhYtD1j82Ipmf6WdIh8/9lGA== X-Received: by 2002:a54:4185:: with SMTP id 5mr2931281oiy.31.1609261721604; Tue, 29 Dec 2020 09:08:41 -0800 (PST) Received: from localhost (189-209-26-110.static.axtel.net. [189.209.26.110]) by smtp.gmail.com with ESMTPSA id l134sm9574416oig.25.2020.12.29.09.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Dec 2020 09:08:41 -0800 (PST) From: Felipe Contreras To: git@vger.kernel.org Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , Junio C Hamano , Jonathan Nieder , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Felipe Contreras Subject: [PATCH v2 1/3] completion: bash: add __git_have_func helper Date: Tue, 29 Dec 2020 11:08:35 -0600 Message-Id: <20201229170837.297857-2-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20201229170837.297857-1-felipe.contreras@gmail.com> References: <20201229170837.297857-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This makes the code more readable, and also will help when new code wants to do similar checks. Signed-off-by: Felipe Contreras --- contrib/completion/git-completion.bash | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 463a3124da..869c73ee2c 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3358,15 +3358,19 @@ __git_support_parseopt_helper () { esac } +__git_have_func () { + declare -f $1 >/dev/null 2>/dev/null +} + __git_complete_command () { local command="$1" local completion_func="_git_${command//-/_}" - if ! declare -f $completion_func >/dev/null 2>/dev/null && - declare -f _completion_loader >/dev/null 2>/dev/null + if ! __git_have_func $completion_func && + __git_have_func _completion_loader then _completion_loader "git-$command" fi - if declare -f $completion_func >/dev/null 2>/dev/null + if __git_have_func $completion_func then $completion_func return 0 From patchwork Tue Dec 29 17:08:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Contreras X-Patchwork-Id: 11992513 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.8 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,URIBL_BLOCKED,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 85F37C433DB for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 570CF22209 for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726190AbgL2RJZ (ORCPT ); Tue, 29 Dec 2020 12:09:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgL2RJY (ORCPT ); Tue, 29 Dec 2020 12:09:24 -0500 Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50C10C061793 for ; Tue, 29 Dec 2020 09:08:44 -0800 (PST) Received: by mail-ot1-x32b.google.com with SMTP id d20so12378121otl.3 for ; Tue, 29 Dec 2020 09:08:44 -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=BHfDTQko7xjogX2qIZ9vgKqkR01FLXLg1Z25DmZlx8U=; b=BrPv6Y4ptUNwqT83A7Ny1c130RpQb8/NrbVlAoKANdMYnCRuR+aEBi8RwzY6l1ggrb PvS3FFuuRs+sagpo1dsbhT8ZAnhDJhEZBT1MWwGIZmuIP+DLYZflOHJJd+W1zfe8SJkG OjddNH/c3U+z75fuNwfAwX25d6YGCX64rFaJ001QXObQQ5otPVCPQzEEDuR7iRyGa+3F NbOrYtXhAr5VK9XcqAEuO7lzHNfyOVMtxNJIBKGmyAfrxz2hE9IpSFylYTtjpORtEhC0 WkvpNa01ZLDusl3BCODavckNaiek0rYbFm+r8SvZD+6f6g1+SXAEc6GmtwXSBwHTDz43 5UYQ== 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=BHfDTQko7xjogX2qIZ9vgKqkR01FLXLg1Z25DmZlx8U=; b=XvE10KEdrbUv+UbHre31FQ3ls8Yg/8Rh62vtx7X0GLQp5ZCAe/xagXB91sgpHp2z8d kFlqNRG7k+J19tnDjOXcA6Pl1zNC+ggnZgBRXFHC3X1kpj0yu8j6gCgEdWA4jd7AutPV Zf49cjfQ0Pk9+kf/TcfhN//z/xjmrasgSr3gn/yAuL1ezWqfHoxuUixFvKWG/urDYLaw S3/rUvXssPSY9f/ytEL62WpMIYoJVRZxWV+m6j7Ul/fh9flVARwXOPIFdZIYgO8zDUw0 zzAgDuq/Pr9EwFdCaDg0EkNwbSfQOjrm66M64Q20MFRAcar7dlAzhD0reuJkp07XPLyC EwIQ== X-Gm-Message-State: AOAM533bbRID289d6oSarcdL2VijnEaPja0+83CbbfbNjIQRhHrFpIvq SkQ8nplDDs73TGgMeru117FRMg66c9Wgaw== X-Google-Smtp-Source: ABdhPJwzppEH3lJ7Mdl7QaF16g+oIsGymR4Gxg8uZNSc11DWJGWgAufNJLQYoFjkPvwfEhidcoj/YA== X-Received: by 2002:a05:6830:214c:: with SMTP id r12mr36698115otd.208.1609261723591; Tue, 29 Dec 2020 09:08:43 -0800 (PST) Received: from localhost (189-209-26-110.static.axtel.net. [189.209.26.110]) by smtp.gmail.com with ESMTPSA id z38sm9723824ooi.34.2020.12.29.09.08.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Dec 2020 09:08:43 -0800 (PST) From: Felipe Contreras To: git@vger.kernel.org Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , Junio C Hamano , Jonathan Nieder , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Felipe Contreras Subject: [PATCH v2 2/3] test: completion: add tests for __git_complete Date: Tue, 29 Dec 2020 11:08:36 -0600 Message-Id: <20201229170837.297857-3-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20201229170837.297857-1-felipe.contreras@gmail.com> References: <20201229170837.297857-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Even though the function was marked as not public, it's already used in the wild. We should at least test basic functionality. Signed-off-by: Felipe Contreras --- t/t9902-completion.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index a1c4f1f6d4..c0b4380eae 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2380,4 +2380,12 @@ test_expect_success 'sourcing the completion script clears cached --options' ' verbose test -z "$__gitcomp_builtin_notes_edit" ' +test_expect_success '__git_complete' ' + unset -f __git_wrap__git_main && + __git_complete foo __git_main && + __git_have_func __git_wrap__git_main && + __git_complete gf _git_fetch && + __git_have_func __git_wrap_git_fetch +' + test_done From patchwork Tue Dec 29 17:08:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Contreras X-Patchwork-Id: 11992517 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.8 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,URIBL_BLOCKED,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 AB577C433E6 for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C64122225 for ; Tue, 29 Dec 2020 17:09:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726214AbgL2RJ2 (ORCPT ); Tue, 29 Dec 2020 12:09:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgL2RJ1 (ORCPT ); Tue, 29 Dec 2020 12:09:27 -0500 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 495E2C061798 for ; Tue, 29 Dec 2020 09:08:47 -0800 (PST) Received: by mail-oi1-x235.google.com with SMTP id 15so15225934oix.8 for ; Tue, 29 Dec 2020 09:08:47 -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=AzyrYwUzR99rlHvRQUbf4IquxyYXFyP02eQV6zlQddI=; b=R1N9NAzlvV0QQZGoMw9GsPknXGhqydr6hT6pMhuByFtoIvn3dF9wy92q+ZtQ4pE5N4 m+moKZ4KiUBUSorbwg6CHyHOibbElMIyrQ/uZAwZJ6gRPRktv8mETz2Ub3mZrGVjssa1 SWVcEu+P4cep4GpNxAzDrRhYhjjCa4ngQbS898NaJzwzOMIzcfGd5QO7m8eNW5EBmuuw sMaZF0WN4O2eewkTJwMm/SaXOckQkI/qTMT2sOr4pJLFj3FCaHL57Z3mgMjvSTTtNcPQ r/sYF5xDM4/nkXZP72ko/+1DO7FZ/71nwsOgZzSYLMRJkRnQ85TrNA553lib2Mjkbfq2 4bOQ== 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=AzyrYwUzR99rlHvRQUbf4IquxyYXFyP02eQV6zlQddI=; b=XUFjUNrDeNjUGJ7G/4LUEID9abDxdZljFG+u1yyCs4CduvpAzjpMhAgTBkvkPO2W8Q d6hSCZ5+5ww9uMX9fjIoBUi+a8Ycl4Tc3dsbRYMu+K0C3E26hvorx9eec4sgq718yucN wHDmGGqooz8RCwZ9vGJpu3z8lyPSbdivrzE3G4+uN1xVpGbwyAG3L8PZpdEMOB7xakv4 sAsyT1VFl1v8xRTq0umJPv2QxNCAUs9u0aUBJTkftJzu2vKnhxU4IBVQm2p7ngjlr479 ZBo4gfG2ADjKjC8drkS6RJGqe4BvkzRNpqLhpFFpsgrHr9Mefk4aZOalfTdrYWQA76W9 cEQQ== X-Gm-Message-State: AOAM530TIfmbfiJkNZ7/bz27OJWXBZ+8NTlg9f8lo0YOb7vreohHSBbL 3jj22y1gb2LDSuy4LtmrL0iDosBhLcUFOQ== X-Google-Smtp-Source: ABdhPJx3L4mnzoSibYgv1f14fuOeLle2J1Ts8DRa/BkfO65g5OGXO3mFiZwXSgglb0wZBC/EZPEykQ== X-Received: by 2002:a54:400f:: with SMTP id x15mr2928530oie.38.1609261726511; Tue, 29 Dec 2020 09:08:46 -0800 (PST) Received: from localhost (189-209-26-110.static.axtel.net. [189.209.26.110]) by smtp.gmail.com with ESMTPSA id t19sm10813102otp.36.2020.12.29.09.08.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Dec 2020 09:08:46 -0800 (PST) From: Felipe Contreras To: git@vger.kernel.org Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , Junio C Hamano , Jonathan Nieder , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Felipe Contreras Subject: [PATCH v2 3/3] completion: add proper public __git_complete Date: Tue, 29 Dec 2020 11:08:37 -0600 Message-Id: <20201229170837.297857-4-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20201229170837.297857-1-felipe.contreras@gmail.com> References: <20201229170837.297857-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When __git_complete was introduced, it was meant to be temporarily, while a proper guideline for public shell functions was established (tentatively _GIT_complete), but since that never happened, people in the wild started to use __git_complete, even though it was marked as not public. Eight years is more than enough wait, let's mark this function as public, and make it a bit more user-friendly. So that instead of doing: __git_complete gk __gitk_main The user can do: __git_complete gk gitk And instead of: __git_complete gf _git_fetch Do: __git_complete gf git_fetch Backwards compatibility is maintained. Signed-off-by: Felipe Contreras --- contrib/completion/git-completion.bash | 31 ++++++++++++++++++++------ t/t9902-completion.sh | 14 +++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 869c73ee2c..1179bd2876 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3497,10 +3497,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 ; }" @@ -3508,13 +3505,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 __git_have_func $2; then + func=$2 + elif __git_have_func __$2_main; then + func=__$2_main + elif __git_have_func _$2; 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 c0b4380eae..c4a7758409 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2382,10 +2382,22 @@ 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 && __git_have_func __git_wrap__git_main && + unset -f __git_wrap__git_main && + __git_complete gf _git_fetch && - __git_have_func __git_wrap_git_fetch + __git_have_func __git_wrap_git_fetch && + + __git_complete foo git && + __git_have_func __git_wrap__git_main && + unset -f __git_wrap__git_main && + + __git_complete gd git_diff && + __git_have_func __git_wrap_git_diff && + + test_must_fail __git_complete ga missing ' test_done