From patchwork Thu Nov 30 20:24:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stan Hu X-Patchwork-Id: 13475039 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EDEQ6xYZ" Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E91A1712 for ; Thu, 30 Nov 2023 12:24:26 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1cfc3f50504so12739575ad.3 for ; Thu, 30 Nov 2023 12:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701375865; x=1701980665; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l0qe0jUziZXy/T3wO6y/hjgcYSR3PIAXB6KWk1Hr6QI=; b=EDEQ6xYZDlp9z5+bi9hGLRpbT46nG2Sqhm+dW5B4kpbzVQBsuPfnD9lmRHx+FlOkJd t201L7wKw2Wb/G91/yid/G7+4rKhCTjZGG0yIl/pQLXVJonqNB+MqYhAkqH8IGkPv3x/ 68cbNUiMLMtRWQDGR1E1XVVnx4MyhMhw7Tkx5S76kLesTkYvOZRfOPQtdRVzKz9fm+HA 4Mjpx+CGFFVrpPk/iTaspNYB5kBNLrVuBv8bJTSht/bVXtc6g1qN1zdPiGwFyfO9epqH mrs0/GjjSh53/6kv7J4rMk9G8ZUvD4xa4wJelJAhDbcT9SwOP9m/RG3ZdpLYvZpaub6V 3gEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701375865; x=1701980665; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l0qe0jUziZXy/T3wO6y/hjgcYSR3PIAXB6KWk1Hr6QI=; b=onb7NyT3Oed1M6oVsZ/m+UE7fcZoVGvaVnBw19u+rI8kmm7WpJ1dAtPugbjfIlo0yc zTQBXTj/pLFQjDRom2bej+6hG/6JIoGvWMg9NxiYVxnaNvjNO66uUdO+f77pv9gCB5D9 p2c1F5BdPmtrcPIH23xy91eGrnQqQtdgC3Bq/Os8cWNS16/reABvKoPrzL5BeGDadWA7 oBZWsPJwf+bQQeXa7kHRMKZULVyGwFQckTZ1cIfhsddGnTUGGwHEjMPhogDx2/1T2Qam 9N8HfxgulToeb77vpYGp7ghK6RS54kPF2UNS8cIwwYy9M2PsTfTalgw6pKiRkjE5UCaC ORmA== X-Gm-Message-State: AOJu0YxEhZMtubFqT64GHlx1y8VA5n75LpiFH65+isGBbnlLi1H+d2TE QvKC+J/7/50gJALA9ZFeMGIYo36JIR4= X-Google-Smtp-Source: AGHT+IEz9sdGi2Yv8zSoX1ArPNWBSMDdnVFxF/b1MgA1655xD/rYMCLuUZl1+6T6T/3u8zqB9elLLA== X-Received: by 2002:a17:902:e842:b0:1cf:cbe4:d286 with SMTP id t2-20020a170902e84200b001cfcbe4d286mr14441439plg.13.1701375865475; Thu, 30 Nov 2023 12:24:25 -0800 (PST) Received: from localhost.localdomain (192-184-144-144.fiber.dynamic.sonic.net. [192.184.144.144]) by smtp.gmail.com with ESMTPSA id a20-20020a170902b59400b001cc2ebd2c2csm1805509pls.256.2023.11.30.12.24.24 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 30 Nov 2023 12:24:25 -0800 (PST) From: Stan Hu To: git@vger.kernel.org Cc: Patrick Steinhardt , Christian Couder , Stan Hu Subject: [PATCH 1/2] completion: refactor existence checks for special refs Date: Thu, 30 Nov 2023 12:24:03 -0800 Message-ID: <20231130202404.89791-2-stanhu@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231130202404.89791-1-stanhu@gmail.com> References: <20231130202404.89791-1-stanhu@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In preparation for the reftable backend, this commit introduces a '__git_ref_exists' function that continues to use 'test -f' to determine whether a given ref exists in the local filesystem. Each caller of '__git_ref_exists' must call '__git_find_repo_path` first. '_git_restore' already used 'git rev-parse HEAD', but to use '__git_ref_exists' insert a '__git_find_repo_path' at the start. Reviewed-by: Patrick Steinhardt Reviewed-by: Christian Couder Signed-off-by: Stan Hu --- contrib/completion/git-completion.bash | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 13a39ebd2e..9fbdc13f9a 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -122,6 +122,15 @@ __git () ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null } +# Runs git in $__git_repo_path to determine whether a ref exists. +# 1: The ref to search +__git_ref_exists () +{ + local ref=$1 + + [ -f "$__git_repo_path/$ref" ] +} + # Removes backslash escaping, single quotes and double quotes from a word, # stores the result in the variable $dequoted_word. # 1: The word to dequote. @@ -1625,7 +1634,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options _git_cherry_pick () { __git_find_repo_path - if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then + if __git_ref_exists CHERRY_PICK_HEAD; then __gitcomp "$__git_cherry_pick_inprogress_options" return fi @@ -2067,7 +2076,7 @@ _git_log () __git_find_repo_path local merge="" - if [ -f "$__git_repo_path/MERGE_HEAD" ]; then + if __git_ref_exists MERGE_HEAD; then merge="--merge" fi case "$prev,$cur" in @@ -2934,6 +2943,7 @@ _git_reset () _git_restore () { + __git_find_repo_path case "$prev" in -s) __git_complete_refs @@ -2952,7 +2962,7 @@ _git_restore () __gitcomp_builtin restore ;; *) - if __git rev-parse --verify --quiet HEAD >/dev/null; then + if __git_ref_exists HEAD; then __git_complete_index_file "--modified" fi esac @@ -2963,7 +2973,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options _git_revert () { __git_find_repo_path - if [ -f "$__git_repo_path"/REVERT_HEAD ]; then + if __git_ref_exists REVERT_HEAD; then __gitcomp "$__git_revert_inprogress_options" return fi @@ -3592,7 +3602,7 @@ __gitk_main () __git_find_repo_path local merge="" - if [ -f "$__git_repo_path/MERGE_HEAD" ]; then + if __git_ref_exists MERGE_HEAD; then merge="--merge" fi case "$cur" in From patchwork Thu Nov 30 20:24:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stan Hu X-Patchwork-Id: 13475040 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QFrVhmjE" Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A426B170F for ; Thu, 30 Nov 2023 12:24:27 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1cf7a8ab047so12743085ad.1 for ; Thu, 30 Nov 2023 12:24:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701375867; x=1701980667; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ela9c9mrQk2j555f2KgLQw43/H5v/SX70WTxP3yF5dw=; b=QFrVhmjEydZnCkyrFxNWSf4+cKtw+TRSRqvggl+tn7/iTBxKHolj/5t7IbLpTfbr+L L8TNu2YwB6Ecfh9DRMKEBoU0NiyRCv9aFtTkDv7wUoY02o3WShA5KYENOcnb8/We8RVq JqGpI0PIPQ8exB5Ojm43ploDXMBWywSZvBWrUmFFZLsJKcj9HJ+XFwZNL+X0vC2lsIUS zae/8QxclxmYydehkfNeup370XIFeB4MrUnEV02EX9P/Y1U8yQL1mUkwx1oGBM73n5z6 0UikaIlOFlOu7lpStXDHQW31ujh5+vxrUvzLCSM+drA03sanq58Zvqy5D9qSls93hPvf V+zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701375867; x=1701980667; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ela9c9mrQk2j555f2KgLQw43/H5v/SX70WTxP3yF5dw=; b=EjfB3qUWKi+p73lmWhlPAbYEyjYOoSDzsyGCy7LPboKl5kezfo/1GnYBdEPHQ15i66 Ok7UHskORvu+v+727Vkc6SleY2SYrQX8EHo2PzmC+wRqNzvQ8V6KIXMDa16aio/3X3ac mBi5myZKWSMudnqhWLvcvkbVLnPun/zW9GFAO4MznmdqVXEMMqI1hfbKzuPyealqBQ1J 7B557DT0DTWNaM2AlF7qfO3g1epAFwU4jfWOK4FcgLGTz2qlgqR8kpzFTdeTPuXRHvB/ oXIhskgZtT+J//kHLf9LYtn5h7DL35tWFxFwh5MEvU2kSJ5HvUe2DSMBm99jACo0CroU h3hw== X-Gm-Message-State: AOJu0YxKdsgoiQZRaZAwcem6Dhi/lWPf4w4B3GRKZSdSeffIn+hHUmNO Ap5O4AhAT2c5pflgonnpneqjOHJ+qvw= X-Google-Smtp-Source: AGHT+IG9a8QVNMWns45Rq2Aqwzt/JUDBhqn4zSp5mIeZMPlbyspLkvd8HMxC/ze70e48Ho/nSEkh2Q== X-Received: by 2002:a17:902:db03:b0:1cf:c518:fa70 with SMTP id m3-20020a170902db0300b001cfc518fa70mr19162611plx.34.1701375866846; Thu, 30 Nov 2023 12:24:26 -0800 (PST) Received: from localhost.localdomain (192-184-144-144.fiber.dynamic.sonic.net. [192.184.144.144]) by smtp.gmail.com with ESMTPSA id a20-20020a170902b59400b001cc2ebd2c2csm1805509pls.256.2023.11.30.12.24.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 30 Nov 2023 12:24:26 -0800 (PST) From: Stan Hu To: git@vger.kernel.org Cc: Patrick Steinhardt , Christian Couder , Stan Hu Subject: [PATCH 2/2] completion: stop checking for reference existence via `test -f` Date: Thu, 30 Nov 2023 12:24:04 -0800 Message-ID: <20231130202404.89791-3-stanhu@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231130202404.89791-2-stanhu@gmail.com> References: <20231130202404.89791-1-stanhu@gmail.com> <20231130202404.89791-2-stanhu@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In contrib/completion/git-completion.bash, there are a bunch of instances where we read special refs like HEAD, MERGE_HEAD, REVERT_HEAD, and others via the filesystem. However, the upcoming reftable refs backend won't use '.git/HEAD' at all but instead will write an invalid refname as placeholder for backwards compatibility, which will break the git-completion script. Update the '__git_ref_exists' function to: 1. Recognize the placeholder '.git/HEAD' written by the reftable backend (its content is specified in the reftable specs). 2. If reftable is in use, use 'git rev-parse' to determine whether the given ref exists. 3. Otherwise, continue to use 'test -f' to check for the ref's filename. Reviewed-by: Patrick Steinhardt Reviewed-by: Christian Couder Signed-off-by: Stan Hu --- contrib/completion/git-completion.bash | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 9fbdc13f9a..f5b630ba99 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -122,12 +122,35 @@ __git () ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null } +# Helper function to read the first line of a file into a variable. +# __git_eread requires 2 arguments, the file path and the name of the +# variable, in that order. +# +# This is taken from git-prompt.sh. +__git_eread () +{ + test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1" +} + # Runs git in $__git_repo_path to determine whether a ref exists. # 1: The ref to search __git_ref_exists () { local ref=$1 + # If the reftable is in use, we have to shell out to 'git rev-parse' + # to determine whether the ref exists instead of looking directly in + # the filesystem to determine whether the ref exists. Otherwise, use + # Bash builtins since executing Git commands are expensive on some + # platforms. + if __git_eread "$__git_repo_path/HEAD" head; then + b="${head#ref: }" + if [ "$b" == "refs/heads/.invalid" ]; then + __git -C "$__git_repo_path" rev-parse --verify --quiet "$ref" 2>/dev/null + return $? + fi + fi + [ -f "$__git_repo_path/$ref" ] }