From patchwork Wed May 27 11:38:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Keller X-Patchwork-Id: 11572665 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 8B05F166C for ; Wed, 27 May 2020 11:38:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7804020888 for ; Wed, 27 May 2020 11:38:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726693AbgE0Lir (ORCPT ); Wed, 27 May 2020 07:38:47 -0400 Received: from mga18.intel.com ([134.134.136.126]:3342 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730085AbgE0Lig (ORCPT ); Wed, 27 May 2020 07:38:36 -0400 IronPort-SDR: XPVTqD+YjARHeM623Sgo8v1OX+o5ecsATkYYJKPAJR1fK8hVpN1bahaAHxPz6+rFdKZ8nYg7P6 BuCWcIHeMRig== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2020 04:38:34 -0700 IronPort-SDR: OOXt+TCAXAKjg0DkbdIFgWJ8BHY0W9QuJOJ88HLluup93v1wM6TnQ63R1IHH+LFyMBzpmXhEZ0 W9iMYiEcJs3Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,441,1583222400"; d="scan'208";a="302431796" Received: from jekeller-desk.amr.corp.intel.com ([10.166.241.33]) by orsmga008.jf.intel.com with ESMTP; 27 May 2020 04:38:34 -0700 From: Jacob Keller To: git@vger.kernel.org Cc: Jonathan Nieder , Jacob Keller Subject: [PATCH v2 2/9] completion: improve handling of DWIM mode for switch/checkout Date: Wed, 27 May 2020 04:38:24 -0700 Message-Id: <20200527113831.3294409-3-jacob.e.keller@intel.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200527113831.3294409-1-jacob.e.keller@intel.com> References: <20200527113831.3294409-1-jacob.e.keller@intel.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jacob Keller Both git switch and git checkout can "Do What I Mean" by interpreting a name of unique remote branch as "please create and switch to a local branch named after and tracking this remote branch". Completion supports this DWIM mode by allowing to complete these unique remote branch names. The current logic for when we decide to enable this DWIM mode is not consistent between git checkout and git switch. In both cases, this logic is enabled by default. Then, if any of '--track', '--no-track', or '--no-guess' are provided on the command line, then we disable it. Additionally, this logic is disabled when GIT_COMPLETION_CHECKOUT_NO_GUESS is set to 1. For _git_switch, we also unconditionally enable this logic if "--guess" is provided on the command line. The rules for when to enable or disable DWIM have gotten quite complicated, so its time to refactor these into a function, __git_checkout_default_dwim_mode(). First, the default is to enable DWIM logic. Then, if '--no-track' is provided or if the GIT_COMPLETION_CHECKOUT_NO_GUESS variable is set, we change to default to disabled. A new helper, __git_find_last_on_cmdline is introduced, similar to the already existing __git_find_on_cmdline, but which operates in reverse, finding the *last* matching word of the provided wordlist. This is then used to find the last --guess or --no-guess. In this way, if --guess was last specified (and thus git option parsing will think --guess is enabled), we will unconditionally enable DWIM mode. If --no-guess is provided last, then we will unconditionally disable DWIM mode. Finally, if neither are specified, we use the default. This new logic is more robust, as we will correctly identify superseded options, and ensure that both _git_switch and _git_checkout enable DWIM in similar ways. Add several tests which demonstrate the new expected behavior. Note that some of the git switch tests are marked as failures because the default git switch completion with --guess is sub-par as discussed in a previous commit. This will be fixed in a future change. Signed-off-by: Jacob Keller --- contrib/completion/git-completion.bash | 95 ++++++++++++++++++++----- t/t9902-completion.sh | 98 ++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 19 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 2972df4cb4c9..ed966f5e2991 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1104,6 +1104,40 @@ __git_find_on_cmdline () done } +# Similar to __git_find_on_cmdline, except that it loops backwards and thus +# prints the *last* word found. Useful for finding which of two options that +# supersede each other came last, such as "--guess" and "--no-guess". +# +# Usage: __git_find_last_on_cmdline [