From patchwork Wed Oct 7 07:56:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11819915 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 6D4586CB for ; Wed, 7 Oct 2020 07:56:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 469462083B for ; Wed, 7 Oct 2020 07:56:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LNbwK3NK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727821AbgJGH42 (ORCPT ); Wed, 7 Oct 2020 03:56:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727647AbgJGH41 (ORCPT ); Wed, 7 Oct 2020 03:56:27 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65FCAC061755 for ; Wed, 7 Oct 2020 00:56:27 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id x5so643992pjv.3 for ; Wed, 07 Oct 2020 00:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=IEBOZbXX6goiKh3UqhsWJVdxvu/ESMP4O+77qY0N2D8=; b=LNbwK3NKfxst5uYNLJzX/qYICwRycdSUfOf56lrqnGvAiedgur81ZquYrixs4aN8Ca ZHImVFO4dg+6ALnDa92jRT+2zmImabReDfeusmS+uLcHgBi0qtbitI0EjfBh1jFx8v/q Q6/c+4iW/ZlJFDODwId+EY+2iltpkHW4Quf4lTPeuXG2xLANKvaP8/5csWVzySm0AShw HcjzjDeJphfxIJsp1bOtK4QJGe0+8lq9YQip+8wtry7tHdCKsc+HGHMrx1WusPtW3XN2 lo3dqYo806fTgdrdaUsLywB4lvovOBIUJO/rMrhSl9nADVcz9tky/wCejz6gIyf/9K2A a/YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IEBOZbXX6goiKh3UqhsWJVdxvu/ESMP4O+77qY0N2D8=; b=UUb0drvilUQsQxrtUAdnlOy9IiYsawY60WFmnbY3QTSnZ2koNu59lcNH9jHACnug1y tfyEtXRM6C7yJqPUCtMXHxMrjJZUXTHR6u6OnlpJQdLcYUGyMyvAxuWAqR5M+uw4P1zS 1r1mCZgpaPzWchZ09Z5irAge7mYlDDmAaAKACxgA04kcXxAVlj9/izvWKDZSenB4/DSE Re2QYRXAbLwgn7xRYv7Yw8NRlGCgqXhV9iZfl+v1BBsiOQvGYVXKJf1tp8jg5cfu6aCz q5UEfCFsAhPmcFiHBXZ5nSvx+vb19Px5NkSx3fxQpSBmPqviU+2V7aQFa9THcrX3P/UK ZSog== X-Gm-Message-State: AOAM530EBj96FP1ia1hZQkpOgCfW+paZgS4yPia6fk9zBWxJotg5BY5j M7zuzJy1KU/DTTBCDgUUXqNnuJ7S+Eg= X-Google-Smtp-Source: ABdhPJwqdsM2MLnx46Il6SzBoqh5hsh6tLElXZEQau4h5f297NuuAtLxoEYchAfuH1jRzP5ebjT+rQ== X-Received: by 2002:a17:90b:902:: with SMTP id bo2mr1778662pjb.169.1602057386609; Wed, 07 Oct 2020 00:56:26 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net (c-67-188-114-10.hsd1.ca.comcast.net. [67.188.114.10]) by smtp.gmail.com with ESMTPSA id n9sm1909950pgi.2.2020.10.07.00.56.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Oct 2020 00:56:25 -0700 (PDT) From: Denton Liu To: Git Mailing List Subject: [PATCH v2 1/4] builtin/checkout: fix `git checkout -p HEAD...` bug Date: Wed, 7 Oct 2020 00:56:15 -0700 Message-Id: <723d74febe967ab0142ab1d931e1cac7b6e34461.1602057332.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.28.0.942.g77c4c6094c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Running `git checkout -p` with a merge-base rev results in an error: $ git checkout -p HEAD... usage: git diff-index [-m] [--cached] [] [...] common diff options: -z output diff-raw with lines terminated with NUL. -p output patch format. -u synonym for -p. --patch-with-raw output both a patch and the diff-raw format. --stat show diffstat instead of patch. --numstat show numeric diffstat instead of patch. --patch-with-stat output a patch and prepend its diffstat. --name-only show only names of changed files. --name-status show names and status of changed files. --full-index show full object name on index lines. --abbrev= abbreviate object names in diff-tree header and diff-raw. -R swap input file pairs. -B detect complete rewrites. -M detect renames. -C detect copies. --find-copies-harder try unchanged files as candidate for copy detection. -l limit rename attempts up to paths. -O reorder diffs according to the . -S find filepair whose only one side contains the string. --pickaxe-all show all files diff when -S is used and hit is found. -a --text treat all files as text. Cannot close git diff-index --cached --numstat --summary HEAD... -- () at /libexec/git-core/git-add--interactive line 183. This happens because checkout passes the literal argument (in the example, `HEAD...`) to diff-index which does not recognise merge-base revs. Fix this by using the hex of the found commit instead of the given name. Note that "HEAD" is handled specially in run_add_interactive() so it's explicitly not changed. Signed-off-by: Denton Liu --- builtin/checkout.c | 15 ++++++++++++++- t/t2016-checkout-patch.sh | 7 +++++++ t/t2071-restore-patch.sh | 8 ++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 0951f8fee5..2e69ed24f2 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -471,6 +471,19 @@ static int checkout_paths(const struct checkout_opts *opts, if (opts->patch_mode) { const char *patch_mode; + const char *rev = new_branch_info->name; + char rev_oid[GIT_MAX_HEXSZ + 1]; + + /* + * Since rev can be in the form of `...` (which is not + * recognized by diff-index), we will always replace the name + * with the hex of the commit (whether it's in `...` form or + * not) for the run_add_interactive() machinery to work + * properly. However, there is special logic for the HEAD case + * so we mustn't replace that. + */ + if (rev && strcmp(rev, "HEAD")) + rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid); if (opts->checkout_index && opts->checkout_worktree) patch_mode = "--patch=checkout"; @@ -481,7 +494,7 @@ static int checkout_paths(const struct checkout_opts *opts, else BUG("either flag must have been set, worktree=%d, index=%d", opts->checkout_worktree, opts->checkout_index); - return run_add_interactive(new_branch_info->name, patch_mode, &opts->pathspec); + return run_add_interactive(rev, patch_mode, &opts->pathspec); } repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); diff --git a/t/t2016-checkout-patch.sh b/t/t2016-checkout-patch.sh index 47aeb0b167..999620e507 100755 --- a/t/t2016-checkout-patch.sh +++ b/t/t2016-checkout-patch.sh @@ -59,6 +59,13 @@ test_expect_success PERL 'git checkout -p HEAD with change already staged' ' verify_state dir/foo head head ' +test_expect_success PERL 'git checkout -p HEAD^...' ' + # the third n is to get out in case it mistakenly does not apply + test_write_lines n y n | git checkout -p HEAD^... && + verify_saved_state bar && + verify_state dir/foo parent parent +' + test_expect_success PERL 'git checkout -p HEAD^' ' # the third n is to get out in case it mistakenly does not apply test_write_lines n y n | git checkout -p HEAD^ && diff --git a/t/t2071-restore-patch.sh b/t/t2071-restore-patch.sh index 98b2476e7c..b5c5c0ff7e 100755 --- a/t/t2071-restore-patch.sh +++ b/t/t2071-restore-patch.sh @@ -60,6 +60,14 @@ test_expect_success PERL 'git restore -p --source=HEAD^' ' verify_state dir/foo parent index ' +test_expect_success PERL 'git restore -p --source=HEAD^...' ' + set_state dir/foo work index && + # the third n is to get out in case it mistakenly does not apply + test_write_lines n y n | git restore -p --source=HEAD^... && + verify_saved_state bar && + verify_state dir/foo parent index +' + test_expect_success PERL 'git restore -p handles deletion' ' set_state dir/foo work index && rm dir/foo && From patchwork Sun Oct 4 12:30:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11815647 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 E554D618 for ; Sun, 4 Oct 2020 12:31:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6C4B206A1 for ; Sun, 4 Oct 2020 12:31:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OjPSgwqg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726020AbgJDMao (ORCPT ); Sun, 4 Oct 2020 08:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725840AbgJDMan (ORCPT ); Sun, 4 Oct 2020 08:30:43 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFCFFC0613CF for ; Sun, 4 Oct 2020 05:30:43 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id m34so3944667pgl.9 for ; Sun, 04 Oct 2020 05:30:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=U4n2v+dJHIiHGZtBxeT6+F0Xumd7zVgfX09RJlpl2GA=; b=OjPSgwqgenC7ZbOcrssXFQjWrBSRyawOxXq6GPyhyIFTWnhZ+GCzdF+IOjjjj3Xbzw gKTfrGORuzm7A+1mAvbz7D5vwibpxTbOAZ2PnMjbafJAFWOXhAeu4XN+RvISzaM6VjhW 7+QbWP3UxZLM+BWozDpstlOFjyxZTXx66qLJ+rOUe2SaI3HH2kwLFGx8cCYj2Fg11/pi SvKBI1/ZdZ9EyJcERGDHOv0D5vRFvpnD5xunfk5VLQv9kTFbi2Be2/1KU42Dm5iKFkXJ D9aUEhyii3LgKhDXTlDpe5WY5nB3+VG5TKb9En1angblVNUVtW7HYY35YKFPiCSIUBJe SHFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=U4n2v+dJHIiHGZtBxeT6+F0Xumd7zVgfX09RJlpl2GA=; b=uBO4Sv5yTf6dEROm+lvU+26W3BdOpSYBLUrmDXYH0qngEvAZemCGukN3ytOmZxvbxc 5HvSPmU4DzmtO7bmOOJbQqDm8C72ug88/RJuttaU/v8+qT5P/NX/1RqsGql0q4B7Nh+3 gRcryv4XR1Way8firUr9qMVk7jl0fura99+i9JQwZcFEQ26EDKiZAiKQZdJYll2ctfm7 QAOqmDBNtx/7G12Hy1WmLTjOMECiA0xpKnZaSI1Hg2SUxNGkMhqOBlGTRoIueRsRCzWN ECNud8LZyvscRKdpZW+3P/JxpIXvNwnW1robnDtZXVjH9RLWE1fAo6J55dhJ+80bK0ar 5t0w== X-Gm-Message-State: AOAM530bdpOwfd0N77UYJ/CLAKGzo+D17hSQ1/YtCaCRWCaINcoja86w a+o4d6XrLnb6pG6w4GG9pkOSUxDTKRc= X-Google-Smtp-Source: ABdhPJyACrqD+U66aQA3s/QD3VFCYNySkb51RcyX9hl5l0evfKA0NDN4UXOWBo1WTKLSobfzXtCFEw== X-Received: by 2002:a63:ff1b:: with SMTP id k27mr9216499pgi.230.1601814643093; Sun, 04 Oct 2020 05:30:43 -0700 (PDT) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:4201:c540::414c]) by smtp.gmail.com with ESMTPSA id e8sm7372299pgj.8.2020.10.04.05.30.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Oct 2020 05:30:42 -0700 (PDT) From: Denton Liu To: Git Mailing List Subject: [PATCH 2/2] Doc: document "A...B" form for in checkout and switch Date: Sun, 4 Oct 2020 05:30:34 -0700 Message-Id: <75920c1a25d71899a98fa8c46091446306125842.1601814601.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.28.0.942.g77c4c6094c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Using "A...B" has been supported for the argument for a while. However, its support has never been explicitly documented. Explicitly document it so that users know that it is available. Signed-off-by: Denton Liu --- Documentation/git-checkout.txt | 4 ++++ Documentation/git-restore.txt | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index afa5c11fd3..6ae8bf8dee 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -351,6 +351,10 @@ leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. :: Tree to checkout from (when paths are given). If not specified, the index will be used. ++ +As a special case, you may use `"A...B"` as a shortcut for the +merge base of `A` and `B` if there is exactly one merge base. You can +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. \--:: Do not interpret any more arguments as options. diff --git a/Documentation/git-restore.txt b/Documentation/git-restore.txt index 84c6c40010..55bde91ef9 100644 --- a/Documentation/git-restore.txt +++ b/Documentation/git-restore.txt @@ -40,6 +40,10 @@ OPTIONS + If not specified, the contents are restored from `HEAD` if `--staged` is given, otherwise from the index. ++ +As a special case, you may use `"A...B"` as a shortcut for the +merge base of `A` and `B` if there is exactly one merge base. You can +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. -p:: --patch::