From patchwork Thu Oct 17 17:34:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 11196425 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 10A7913BD for ; Thu, 17 Oct 2019 17:35:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E2D3F20820 for ; Thu, 17 Oct 2019 17:35:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Hy0DvcXR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2440494AbfJQRfP (ORCPT ); Thu, 17 Oct 2019 13:35:15 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:54755 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393451AbfJQRfN (ORCPT ); Thu, 17 Oct 2019 13:35:13 -0400 Received: by mail-wm1-f68.google.com with SMTP id p7so3408303wmp.4 for ; Thu, 17 Oct 2019 10:35:11 -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=fJNVAdvf/H1r6geV9N2K08yheIPQq9KqXpWFm2eAaGM=; b=Hy0DvcXRldwC3+ZKVBXmLPsD3jHUblensKJgRFzL/AGloaEVKBn0qaFVKuL7u7YnG3 lIREq9y93QMWRDClBneVn0NBNLu7j99QrQ29DlTbSxbYZxrHqPWDuxE27kVt09aPwFzB DUjLidWXO36NA3qhQoDXcy5W3MsH07CDewL4gHIjrgZLfsO/wbp46ZpgJDhr5qZAghel MAOOb/wGTj62Do/cr+CL5wbfeLd1l0r/qavoFMJjH8E8INQ+GYlhtex1+hEP0Zq5B2L7 er7ML7GXQ4ODNRD8awJe5zhkuRGBYUJD4j5Hn30MiEUfelOLfAlnAoj/ZFOtr0Mggszn FsZw== 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=fJNVAdvf/H1r6geV9N2K08yheIPQq9KqXpWFm2eAaGM=; b=q7YfB7xv9wIc8lL/PSjAjYRdqdG6Dg9sC6xCjuJ46ahlstWxD6SBn3W2ngdLutisiX du7AmVRY4eCvOE6z8O7mUPcqrNeCqw4V/f8z6DZBUDjIwzHse3eKvTAy3KKrsOFGTvy3 5/HuA9o2J0O/CcwWZZVC60SJ0t6Z9GPFcF+pIiBzDW42r7uLCayXijJo6BtNXvs50Vyg TgBwjylsJctXpUOxB6B63XVnRjEE8va5nMzndrCR+m2IFN0/4ZUrpZPyG3AlyFQ2+Gbp vOHeFK2Y1rJgDTXfzCcqiOHfgQfG3T3eP/3p2f5ns7Cru2FiKqrwsQ5UCgtwjT148uOA kMgw== X-Gm-Message-State: APjAAAXAN/F64ScVsTcXfCy5SEDX5lDkAk0AuhFl+EsrN2kVdMUJtOE1 VGVZOk6+Oh3qyXev39xvj2I= X-Google-Smtp-Source: APXvYqysEDeNXHo812CzR+PXhLlblpfuPMaPhLk41QY5a74gQ0KKa8e9B3hI9ECwthQX9nKlKTMGKQ== X-Received: by 2002:a05:600c:143:: with SMTP id w3mr3624169wmm.35.1571333710807; Thu, 17 Oct 2019 10:35:10 -0700 (PDT) Received: from localhost.localdomain (x4db41bda.dyn.telefonica.de. [77.180.27.218]) by smtp.gmail.com with ESMTPSA id f20sm2599876wmb.6.2019.10.17.10.35.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Oct 2019 10:35:10 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: git@vger.kernel.org, =?utf-8?q?SZEDER_G=C3=A1bor?= Subject: [PATCH 1/6] t9902-completion: add tests for the __git_find_on_cmdline() helper Date: Thu, 17 Oct 2019 19:34:56 +0200 Message-Id: <20191017173501.3198-2-szeder.dev@gmail.com> X-Mailer: git-send-email 2.23.0.1084.gae250eaa40 In-Reply-To: <20191017173501.3198-1-szeder.dev@gmail.com> References: <20191017173501.3198-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The following two patches will refactor and extend the __git_find_on_cmdline() helper function, so let's add a few tests first to make sure that its basic behavior doesn't change. Signed-off-by: SZEDER Gábor --- t/t9902-completion.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 54f8ce18cb..847ce601d2 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -1363,6 +1363,34 @@ test_expect_success 'teardown after path completion tests' ' BS\\dir '$'separators\034in\035dir'' ' +test_expect_success '__git_find_on_cmdline - single match' ' + echo list >expect && + ( + words=(git command --opt list) && + cword=${#words[@]} && + __git_find_on_cmdline "add list remove" >actual + ) && + test_cmp expect actual +' + +test_expect_success '__git_find_on_cmdline - multiple matches' ' + echo remove >expect && + ( + words=(git command -o --opt remove list add) && + cword=${#words[@]} && + __git_find_on_cmdline "add list remove" >actual + ) && + test_cmp expect actual +' + +test_expect_success '__git_find_on_cmdline - no match' ' + ( + words=(git command --opt branch) && + cword=${#words[@]} && + __git_find_on_cmdline "add list remove" >actual + ) && + test_must_be_empty actual +' test_expect_success '__git_get_config_variables' ' cat >expect <<-EOF && From patchwork Thu Oct 17 17:34:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 11196427 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D888313BD for ; Thu, 17 Oct 2019 17:35:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B7AEC21835 for ; Thu, 17 Oct 2019 17:35:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="M15YGFj4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436799AbfJQRfP (ORCPT ); Thu, 17 Oct 2019 13:35:15 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:38656 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728639AbfJQRfO (ORCPT ); Thu, 17 Oct 2019 13:35:14 -0400 Received: by mail-wr1-f68.google.com with SMTP id o15so2881344wru.5 for ; Thu, 17 Oct 2019 10:35:12 -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=PD8Yp0H1cNQecXGlh2bWlADzZm0AD4xK+EpyxV/pLRU=; b=M15YGFj4DSRFYqKaBj1EE3AiKjQkP8bTwj+8VjNIuvQaz4y4dEMZ5///pldNm50f7F 3fNaOh4eLlgTzhXfhd9p6BRRgfkW1dryvd0f0rMDgkspgrcjFs6WKnlKuH+w88O6SkXv 5Qpakk5TuO7so0aYJ8oqmiSMZDITn6FoznCD5l0STJM80bQDnfQwLD9nrIwQQlLmSqLq H6HkZkIoWct2lXXtdL49JGafKCiLajEaw68P7jD+rAmrqCixZx3jd0/Cb8lqj0b0NHVK 1b4XBzeGw82kpjgHHiAXCyIC/xwxDgivOI2a+woSsZl8Bt7dmNkSwJT6yNzk6z0M4/A3 Cyyg== 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=PD8Yp0H1cNQecXGlh2bWlADzZm0AD4xK+EpyxV/pLRU=; b=TF/I+UFontD8sMtBNaINyPQgWGQNI3qez9NZy1Pv+AqIdTv31Bl/fvujd1y2hlz7ay KHHBMzQ/jX8iU4O7b6la9snxZgOfEBtLFfSjGl3snR9E3v9oM/vvYYqMC7oXxyMxDJ+8 E7gErKk2StFCJL0cqL8ziOgvtmL4vIxK8rxgICdB7MRoRMxJiayzExIE827L54Kd+DXt 8dgDyw1+G8fmyahKZymoT3UIS85GdpJ+iWvTfEE/DeGuKt2nk5CXW947HGhH1PC4d87v GELpRnU6h23fz2L5XKfi2Hl81X8RZclyvfoUV/pr2hj85x2RidijyW/EEaqJCZhgXq6k BQZw== X-Gm-Message-State: APjAAAUE1vhYETs0Z+nEHDuguUacd5iSK2RCW9Mf4pk/gtbIxTClOrkf MGAvXIVsspRTsL9gVddL6J3it6Iv X-Google-Smtp-Source: APXvYqzArdXVAXzih8VJ+xbP0fKM5xDrTgPumsu3ii1GM4O9qPWeeDP9FkWntMpCUgwKu8KGFgZvIw== X-Received: by 2002:adf:9dca:: with SMTP id q10mr4094290wre.183.1571333712320; Thu, 17 Oct 2019 10:35:12 -0700 (PDT) Received: from localhost.localdomain (x4db41bda.dyn.telefonica.de. [77.180.27.218]) by smtp.gmail.com with ESMTPSA id f20sm2599876wmb.6.2019.10.17.10.35.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Oct 2019 10:35:11 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: git@vger.kernel.org, =?utf-8?q?SZEDER_G=C3=A1bor?= Subject: [PATCH 2/6] completion: clean up the __git_find_on_cmdline() helper function Date: Thu, 17 Oct 2019 19:34:57 +0200 Message-Id: <20191017173501.3198-3-szeder.dev@gmail.com> X-Mailer: git-send-email 2.23.0.1084.gae250eaa40 In-Reply-To: <20191017173501.3198-1-szeder.dev@gmail.com> References: <20191017173501.3198-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The __git_find_on_cmdline() helper function started its life as __git_find_subcommand() [1], but it served a more general purpose than looking for subcommands, so later it was renamed accordingly [2]. However, that rename didn't touch the body of the function, and left the $subcommand local variable behind, still reminiscent of the function's original purpose. Let's clean up the names of __git_find_on_cmdline()'s local variables and get rid of that $subcommand variable name. While at it, add a short comment describing the function's purpose. [1] 3ff1320d4b (bash: refactor searching for subcommands on the command line, 2008-03-10), [2] 918c03c2a7 (bash: rename __git_find_subcommand() to __git_find_on_cmdline(), 2009-09-15) Signed-off-by: SZEDER Gábor --- contrib/completion/git-completion.bash | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 00fbe6c03d..2384f91e78 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1070,14 +1070,17 @@ __git_aliased_command () } # __git_find_on_cmdline requires 1 argument +# Check whether one of the given words is present on the command line, +# and print the first word found. __git_find_on_cmdline () { - local word subcommand c=1 + local word c=1 + local wordlist="$1" + while [ $c -lt $cword ]; do - word="${words[c]}" - for subcommand in $1; do - if [ "$subcommand" = "$word" ]; then - echo "$subcommand" + for word in $wordlist; do + if [ "$word" = "${words[c]}" ]; then + echo "$word" return fi done From patchwork Thu Oct 17 17:34:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?SZEDER_G=C3=A1bor?= X-Patchwork-Id: 11196429 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 46996912 for ; Thu, 17 Oct 2019 17:35:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2584921835 for ; Thu, 17 Oct 2019 17:35:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EMN8XnDR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2440540AbfJQRfQ (ORCPT ); Thu, 17 Oct 2019 13:35:16 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:40899 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436664AbfJQRfP (ORCPT ); Thu, 17 Oct 2019 13:35:15 -0400 Received: by mail-wr1-f67.google.com with SMTP id o28so3304858wro.7 for ; Thu, 17 Oct 2019 10:35:14 -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=iAmHQa7BbpSDWk6c3is6+AZz0YpZ+C3LyS2a5t+dt24=; b=EMN8XnDR0hdESm8MweJxso1Faw/7+jDa0So/Di6tJ3j3H/PuI8C7YFFUCRrx+2PMsJ it8yuF67MENe9WevUY90Na4LJEewo9pR3ylUtmxKrEKYqDmeWDQupFfsVtea61I2aMWg QBEmdj7TVZP0FS6jUk6gid0l3zAXdOBUphY3Qm3733VazDXF8TmVGMNNXE/0rdknhrC6 rNzL2XmaFZWXWWl+3IzWjLNKJGAZeV0Wums5TsdfU8e7et07Aof7XzjD/4DK4/us2VcI PzdXSrchZ535QRbiBDmhnbvL4CFRusxm5qVUW0AEBmvNbFJ2fSwIKvVu8JaWQW6yfTDx m3GQ== 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=iAmHQa7BbpSDWk6c3is6+AZz0YpZ+C3LyS2a5t+dt24=; b=ShQh692ZW3Y0H2nbqHzzLcgz+t2U3VRj+ghd27KZbHCuIuYeGCUcZTZdhUkjROkpmb qXuvWMmKsQdamqNlEBeb4E5ZzRygfUXugCPMN501PM6KwTM/CgTtdECoQEsaqzNiMZnO LXIw6I1GcrR/YucZdPpFlVJDfU4p4q/7ibzg6to+jZOYsr1+rY7o7X0MnXGU8wSyZjpe 4+PNpIlIXfHWDyM/tWlyrZ1zpSIG8RQb9ub6bkaY4XAE50IA7QBLrt8yBSuCOvGsLz0L duMZUxykxFN9kH+0V+qg05DROSNETdgA9arXDL7SBvCaqGc/CupCdQFky86miuaGFrol WINw== X-Gm-Message-State: APjAAAVpgOIsUHYxjITmZqlx3jlpq3gfVAyHLZUVDBdlwYfYFsefVb0a R3VPv+3H5kc9aLoE94YsYvTTLFPB X-Google-Smtp-Source: APXvYqzdCL77r5wvWZVLmQybBivWYfxuEfqucEuJd4jWM0O2hHKGOX1MmLQNO8mBiZkYPx0jWXvTjg== X-Received: by 2002:adf:e292:: with SMTP id v18mr4030206wri.190.1571333713598; Thu, 17 Oct 2019 10:35:13 -0700 (PDT) Received: from localhost.localdomain (x4db41bda.dyn.telefonica.de. [77.180.27.218]) by smtp.gmail.com with ESMTPSA id f20sm2599876wmb.6.2019.10.17.10.35.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Oct 2019 10:35:13 -0700 (PDT) From: =?utf-8?q?SZEDER_G=C3=A1bor?= To: Junio C Hamano Cc: git@vger.kernel.org, =?utf-8?q?SZEDER_G=C3=A1bor?= Subject: [PATCH 3/6] completion: return the index of found word from __git_find_on_cmdline() Date: Thu, 17 Oct 2019 19:34:58 +0200 Message-Id: <20191017173501.3198-4-szeder.dev@gmail.com> X-Mailer: git-send-email 2.23.0.1084.gae250eaa40 In-Reply-To: <20191017173501.3198-1-szeder.dev@gmail.com> References: <20191017173501.3198-1-szeder.dev@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When using the __git_find_on_cmdline() helper function so far we've only been interested in which one of a set of words appear on the command line. To complete options for some of 'git worktree's subcommands in the following patches we'll need not only that, but the index of that word on the command line as well. Extend __git_find_on_cmdline() to optionally show the index of the found word on the command line (IOW in the $words array) when the '--show-idx' option is given. Signed-off-by: SZEDER Gábor --- contrib/completion/git-completion.bash | 20 +++++++++++++++--- t/t9902-completion.sh | 29 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 2384f91e78..55a2d3e174 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1069,18 +1069,32 @@ __git_aliased_command () done } -# __git_find_on_cmdline requires 1 argument # Check whether one of the given words is present on the command line, # and print the first word found. +# +# Usage: __git_find_on_cmdline [