From patchwork Fri Oct 1 10:04:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30498C433F5 for ; Fri, 1 Oct 2021 10:05:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 155E461A6F for ; Fri, 1 Oct 2021 10:05:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353271AbhJAKGv (ORCPT ); Fri, 1 Oct 2021 06:06:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353235AbhJAKGu (ORCPT ); Fri, 1 Oct 2021 06:06:50 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EFCEC06177B for ; Fri, 1 Oct 2021 03:05:06 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id h15so12987662wrc.3 for ; Fri, 01 Oct 2021 03:05:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=adCRBqCVvXxlRTRcD+OFfeQu6DbMvFmosaVnzgCDr5A=; b=JIAAL0ilzNQLD/sFASLFCAjl6hyFf45SUHJ5aGjE0ukJxku7SfENScGutLVI/Cubku JhOcVTABFuXSW0ok68zpAdH1oaQFBQC0bB8AWtEVFn3VLAm2f3wuRfsKhbWekxUQJSFw OhyFp3/B9MLeqnhVyNmdgnQvUvl1+gXLDW4cH+D1RSIa5E73yqF6z90n0PmdI94e4DDK rrWyNPVrO8Hsem4L+4DLz7qZwv9SbyJMgak9DzLnc2qYEmNkWBhUgQ6ksbU8otlJpuT1 Y6GMVPmAgMOny71vA7Km6PxZPDTwpr0KjWKOh+8RLQFBIGlw4GteFQV6ACsiKooqxVMY TJnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=adCRBqCVvXxlRTRcD+OFfeQu6DbMvFmosaVnzgCDr5A=; b=XJGZLzkL4vJmhliXLI8KvNgiAw2GoAFYm0n7ZqjK1NL78YMpMfAyVW8e09o0vzvZzI uqgBDAOZyCeb6aCc695/AQQHmtjM0isWOuR+smoZ0Fr4XKSJiDSDYfx7Qt05UjDXV4og KTwfgJCIHXG4gwoM9xT16R8AGiqUw40cf2ua74eSWB5nXWZAhqqVDYSVYG+5PPTwlHeO 5zKsBEESwuHettgvCADsoRA4dIo6OX/xNDaJg3BQvyTO8ZU3peljnhJbuEfEXoMl+cPc 4huLixcO7G7hPUUqvWp9eMdEUuud4O0fv2zqvRo2zJeZODrvTPp9UwWQL6CjGKxMm623 H72Q== X-Gm-Message-State: AOAM531P3DdJ9j2S4yqVqaE5k7TpAAbYc0/uLAHOoZ6kP5vUxdTwIvDi hdFibtQGvG3BIxfcSFECR9jyZvP9tkk= X-Google-Smtp-Source: ABdhPJyUA0MvIPMhvcvFIG/N4laIOE3bqfOYFdUEVifGter8W11BLEdjGkZo26iNFDJu/352rh7AzQ== X-Received: by 2002:a05:6000:1a8b:: with SMTP id f11mr2834877wry.251.1633082704672; Fri, 01 Oct 2021 03:05:04 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o3sm5350287wra.52.2021.10.01.03.05.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:04 -0700 (PDT) Message-Id: <4d3441c2b2571b91cecca7c28232540b9aaf9993.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:52 +0000 Subject: [PATCH 01/11] rebase: factor out checkout for up to date branch Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood This code is heavily indented and it will be convenient later in the series to have it in its own function. Signed-off-by: Phillip Wood --- builtin/rebase.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index f82bfaed118..f4c312dd8b5 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1099,6 +1099,23 @@ static int rebase_config(const char *var, const char *value, void *data) return git_default_config(var, value, data); } +static int checkout_up_to_date(struct rebase_options *options) +{ + struct strbuf buf = STRBUF_INIT; + int ret = 0; + + strbuf_addf(&buf, "%s: checkout %s", + getenv(GIT_REFLOG_ACTION_ENVIRONMENT), + options->switch_to); + if (reset_head(the_repository, &options->orig_head, "checkout", + options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK, + NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0) + ret = error(_("could not switch to %s"), options->switch_to); + strbuf_release(&buf); + + return ret; +} + /* * Determines whether the commits in from..to are linear, i.e. contain * no merge commits. This function *expects* `from` to be an ancestor of @@ -1978,21 +1995,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (!(options.flags & REBASE_FORCE)) { /* Lazily switch to the target branch if needed... */ if (options.switch_to) { - strbuf_reset(&buf); - strbuf_addf(&buf, "%s: checkout %s", - getenv(GIT_REFLOG_ACTION_ENVIRONMENT), - options.switch_to); - if (reset_head(the_repository, - &options.orig_head, "checkout", - options.head_name, - RESET_HEAD_RUN_POST_CHECKOUT_HOOK, - NULL, buf.buf, - DEFAULT_REFLOG_ACTION) < 0) { - ret = error(_("could not switch to " - "%s"), - options.switch_to); + ret = checkout_up_to_date(&options); + if (ret) goto cleanup; - } } if (!(options.flags & REBASE_NO_QUIET)) From patchwork Fri Oct 1 10:04:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530119 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 684C1C433FE for ; Fri, 1 Oct 2021 10:05:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EAD56134F for ; Fri, 1 Oct 2021 10:05:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353409AbhJAKHE (ORCPT ); Fri, 1 Oct 2021 06:07:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353285AbhJAKG4 (ORCPT ); Fri, 1 Oct 2021 06:06:56 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8116C061775 for ; Fri, 1 Oct 2021 03:05:06 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id u18so14617275wrg.5 for ; Fri, 01 Oct 2021 03:05:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=vWqQaKK/VPFM0wvbIKSOyHH+DQv3Kzr8GXPKthuFnjY=; b=R2gpjvXMCQdqJ2hDIjNjsCBq8OiELt0UCxoD5NRVzYutyg6TlQccrignKOKemFQU26 kYiu/dN3nkBqrIkyHlKbLsWi2hPyYl0qFgir5AvEyEpzBumNXom9deFWqc8wpeeyNumK Sjt43svfXVOWdDYZcPNBdaMMg/UQU43qRmOMND5J/7d4olBW1H/RpRU6Op1hVLV5CRet ZFMD0OYw8sof8BtCAJADTOmKIhmK/1le2fHak/R4rt1pQhiimtcCAFQm7lXKIQCtpg78 vWXB/0rxEyPxR34jBeQKgdiYiRgouVAOGuQqO1XqyLRqTgO3ZXf++mBaPaX7WfvJJD5y GVog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=vWqQaKK/VPFM0wvbIKSOyHH+DQv3Kzr8GXPKthuFnjY=; b=oAUyAUcSgF5c1h62FHEp1mm3sccAeYH02tDXMaNIPBsNBkq35Gow+/1zLccZoYuUxV cTQ0H4DML1SbufBWDJWZJi/r1PthAgPz9ECY6SEMVzo0ixLLheFfbVVdyOdNuo8RzOSa MaJQhBju6BLXc77wzQsN7wQdyHKe4JLq0TmuLLKMBjh/DWB/5uADyrHnB4whiaRF3+bj RK9FHr0OfhsPzttSQlld6Z9HuTdqaFPYxpcWgvdCGTjGkv8z9oSBsmWOJU6ZqcBTZRN4 X42LK2Ecs245SnUvOdp9WoWwgXiCavcqK0yTcwqdMFsTDIwFfnN8Z4SVHGzvW3eq+B4f NLfQ== X-Gm-Message-State: AOAM531HYLV7vdK9c2aPFzGlq1OIcctuYGYP+j8PulCF+d0hzrSHZsg5 Y/AZ0TUdSbDF1t6WO120yQ8d0sSCWkc= X-Google-Smtp-Source: ABdhPJyaCLZk11Ggh7t7niBFQ0q3/VQEf6jWdmGNPiHE/KlP62hvQd9oFJkCYLPnAccFEMVrSbinzg== X-Received: by 2002:adf:eb12:: with SMTP id s18mr11337878wrn.97.1633082705431; Fri, 01 Oct 2021 03:05:05 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l124sm7161048wml.8.2021.10.01.03.05.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:05 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:53 +0000 Subject: [PATCH 02/11] reset_head(): fix checkout Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood The reset bit should only be set if flags contains RESET_HEAD_HARD. The test for `!deatch_head` dates back to the original implementation of reset_head() in ac7f467fef ("builtin/rebase: support running "git rebase "", 2018-08-07) and was correct until e65123a71d ("builtin rebase: support `git rebase `", 2018-09-04) started using reset_head() to checkout when fast-forwarding. Signed-off-by: Phillip Wood --- reset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reset.c b/reset.c index 79310ae071b..fc4dae3fd2d 100644 --- a/reset.c +++ b/reset.c @@ -57,7 +57,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, unpack_tree_opts.update = 1; unpack_tree_opts.merge = 1; init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL); - if (!detach_head) + if (reset_hard) unpack_tree_opts.reset = 1; if (repo_read_index_unmerged(r) < 0) { From patchwork Fri Oct 1 10:04:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530105 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC941C433F5 for ; Fri, 1 Oct 2021 10:05:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92FBD61A50 for ; Fri, 1 Oct 2021 10:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353301AbhJAKGy (ORCPT ); Fri, 1 Oct 2021 06:06:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353235AbhJAKGv (ORCPT ); Fri, 1 Oct 2021 06:06:51 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E146C06177B for ; Fri, 1 Oct 2021 03:05:07 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id u18so14617334wrg.5 for ; Fri, 01 Oct 2021 03:05:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=9tEkhINn7Ua/VjxkmDpw5wJZK+Z/dD2CWTL/hR0YrGA=; b=NWhrEllfuOBqaxQ6WeB08rnJELfRLBlYYr6h1zw1JNCAEoOFiix2Ma8PWb9NON3bhH DYRXIBnq0A0yHj8I65Hw8v6s5i2L+geX0bEuWFEN+uYRivW90gZMPSACuHLVIb3GiOZK f6vBmSU86j+d7bWTo/D3DrSNgP22T25mW5R4JpcjPYFWluHyx2NXyXKvG/kxyOqIq7lI SxaXH9uyNkkdHtVm+Q3pJlLxgKB0pDJU4k0HkyPYBnlD1RZfnXGsxzZzjYmGqxca2yQL rUeBpFZMerdEhWJEAUHqdvMfRaFYgwxYMmWjecLkw0BmFsH4F03iLH7saf3rc8A27zi9 z6RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=9tEkhINn7Ua/VjxkmDpw5wJZK+Z/dD2CWTL/hR0YrGA=; b=FpZGTaJj9P0bfp8Ev7Vt+agy/EYv3N50WLfHEz2mcB+OGiM4kCwcHPPL1i7/+0ysUQ Qf/EFfWGC18oCBInhlTKRv/RwjtXuV6tjQLgA0scuGLLUt/cki5DET3yFNHgz8/+OZNN wl/lvDfZ6MVV/cPOdmMAUOrIvPzy1KvaUgYKOx4LKNIK6N59hUbxr/1rpmfi4jSjOaWy 1wIyMGxLnBLyr9uSQyziHszvpkGvz7RhC7E87R+H5IjfVpG0bzu6fpAVefBweoCm18bT XQLT+xLvie/ltdPdspCL7qsC59gRaweLnUgxHyqkRDMrK6FmhJrKnUEVt+ldAb5m3Csl N/iA== X-Gm-Message-State: AOAM531ZlZX5n+Yviphr0xPInt2kBYWDVve8jI8AmV3r5ivtP2/Pjm9z kklLgWxhil1DDMr8y0hADto7yImAO+U= X-Google-Smtp-Source: ABdhPJx8TcrdT5sNiYYslx194lQuZHHNQjv+cVEiXakMc68kfcUdBOcZ6Qn6Y6m5oyTrFeNl4nFCkQ== X-Received: by 2002:adf:d1eb:: with SMTP id g11mr11173290wrd.31.1633082706079; Fri, 01 Oct 2021 03:05:06 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i92sm5232362wri.28.2021.10.01.03.05.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:05 -0700 (PDT) Message-Id: <28872cbca687b057663a4e3408cb94d69fb60f94.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:54 +0000 Subject: [PATCH 03/11] reset_head(): don't run checkout hook if there is an error Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood The hook should only be run if the worktree and refs were successfully updated. Signed-off-by: Phillip Wood --- reset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reset.c b/reset.c index fc4dae3fd2d..5abb1a5683e 100644 --- a/reset.c +++ b/reset.c @@ -125,7 +125,7 @@ reset_head_refs: ret = create_symref("HEAD", switch_to_branch, reflog_head); } - if (run_hook) + if (!ret && run_hook) run_hook_le(NULL, "post-checkout", oid_to_hex(orig ? orig : null_oid()), oid_to_hex(oid), "1", NULL); From patchwork Fri Oct 1 10:04:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530107 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D8C7C433FE for ; Fri, 1 Oct 2021 10:05:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E78D561A50 for ; Fri, 1 Oct 2021 10:05:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353294AbhJAKGz (ORCPT ); Fri, 1 Oct 2021 06:06:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353273AbhJAKGw (ORCPT ); Fri, 1 Oct 2021 06:06:52 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DC3FC06177C for ; Fri, 1 Oct 2021 03:05:08 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id a131-20020a1c7f89000000b0030d4c90fa87so1133393wmd.2 for ; Fri, 01 Oct 2021 03:05:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=k735m5bUirZ81cg5ccSckBHzq8i4raMoX1EA9nTqePs=; b=HwnWGsth7Ms1M/kkXuUDGXRjd2cJ9Cu3y3B93lvhk3o3tMmPqURMMmLQHhb5gc9Cbm 53XW4mqEzIi6baE/UbKVnuwCuuKzjLMe5Q7wd2xGeDbVJtGUHwYhvb+PsA5RaHmQBOyc 53y9GPOMt4zAlBwABJ/JjHJmoIGMSXrlvljXUCIpJcaVJAgn1T6RmQdctKHBayC2bWBl lNtqC98czLkSIcbCfDX8d/sAAtG5XesHLeDTTc4opIQTricNvKszqhxGFwrVQHKo1j8B gPS7WgM156IWPT34D8dEIDMnPESM8kbweuME3qNNeRQtBDWTvjxbM6WMcXT2dDnNxm8s FvnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=k735m5bUirZ81cg5ccSckBHzq8i4raMoX1EA9nTqePs=; b=wz0sJ7gLSMXCKxCcgH8FMWgB3a2E9Zh7y0cbYrMEcQ65FhtGJt04D9yvpX1NxsLp3o /1syvvonscCvdvLOif7cEeIdXJFM+M4AEJYPIaCD6hTAG7oPutL1Oip/kHPF7z+0buas CNAOjX7NAFj2WQQcNVxxk9Y4yS7WM3Jh0UwjWYiwaQOfBiMoXHs2uf9/B4IcyQYEQ7vg Cf8Mganj3/qQ3fuDvRr+00xRzPF5GuikBvNcomeZzCxb/GpqzVVWPtBskD8BkLdiF1y3 ZMDH3bpGt9xvaxVGzgnD4pRRfEqWn3Bdm0Z+f6HjjgJEef7IcrD48guXxQDMyrzkwffy HyBg== X-Gm-Message-State: AOAM532S++OzS/p2mchy+xSUbiVw9M6KqRK2yoveS4CKUNw0wTRjd68F +WjyEI2RyZqFiM/0Ha8BJ+cV+n38qtQ= X-Google-Smtp-Source: ABdhPJxt732mAveeurzJKf2N9ipano5ueJBZZWMYR9fNwFZbnF2hcxkEB1x8dK5RTzrShFBIcEKvCg== X-Received: by 2002:a05:600c:ac1:: with SMTP id c1mr3501055wmr.99.1633082706732; Fri, 01 Oct 2021 03:05:06 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y11sm6457341wrg.18.2021.10.01.03.05.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:06 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:55 +0000 Subject: [PATCH 04/11] reset_head(): remove action parameter Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood The action parameter is passed as the command name to setup_unpack_trees_porcelain(). All but two cases pass either "checkout" or "reset". The case that passes "reset --hard" should be passing "reset" instead. The case that passes "Fast-forwarded" is only updating HEAD and so does not call unpack_trees(). The value can be determined by checking whether flags contains RESET_HEAD_HARD so it does not need to be specified by the caller. Signed-off-by: Phillip Wood --- builtin/rebase.c | 14 +++++++------- reset.c | 5 +++-- reset.h | 2 +- sequencer.c | 3 +-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index f4c312dd8b5..26f53c55cc4 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -789,7 +789,7 @@ static int move_to_original_branch(struct rebase_options *opts) opts->head_name, oid_to_hex(&opts->onto->object.oid)); strbuf_addf(&head_reflog, "rebase finished: returning to %s", opts->head_name); - ret = reset_head(the_repository, NULL, "", opts->head_name, + ret = reset_head(the_repository, NULL, opts->head_name, RESET_HEAD_REFS_ONLY, orig_head_reflog.buf, head_reflog.buf, DEFAULT_REFLOG_ACTION); @@ -880,7 +880,7 @@ static int run_am(struct rebase_options *opts) free(rebased_patches); strvec_clear(&am.args); - reset_head(the_repository, &opts->orig_head, "checkout", + reset_head(the_repository, &opts->orig_head, opts->head_name, 0, "HEAD", NULL, DEFAULT_REFLOG_ACTION); error(_("\ngit encountered an error while preparing the " @@ -1107,7 +1107,7 @@ static int checkout_up_to_date(struct rebase_options *options) strbuf_addf(&buf, "%s: checkout %s", getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options->switch_to); - if (reset_head(the_repository, &options->orig_head, "checkout", + if (reset_head(the_repository, &options->orig_head, options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK, NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0) ret = error(_("could not switch to %s"), options->switch_to); @@ -1557,7 +1557,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) rerere_clear(the_repository, &merge_rr); string_list_clear(&merge_rr, 1); - if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD, + if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD, NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) die(_("could not discard worktree changes")); remove_branch_state(the_repository, 0); @@ -1575,7 +1575,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (read_basic_state(&options)) exit(1); - if (reset_head(the_repository, &options.orig_head, "reset", + if (reset_head(the_repository, &options.orig_head, options.head_name, RESET_HEAD_HARD, NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) die(_("could not move back to %s"), @@ -2064,7 +2064,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&msg, "%s: checkout %s", getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); - if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL, + if (reset_head(the_repository, &options.onto->object.oid, NULL, RESET_HEAD_DETACH | RESET_ORIG_HEAD | RESET_HEAD_RUN_POST_CHECKOUT_HOOK, NULL, msg.buf, DEFAULT_REFLOG_ACTION)) @@ -2082,7 +2082,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&msg, "rebase finished: %s onto %s", options.head_name ? options.head_name : "detached HEAD", oid_to_hex(&options.onto->object.oid)); - reset_head(the_repository, NULL, "Fast-forwarded", options.head_name, + reset_head(the_repository, NULL, options.head_name, RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, DEFAULT_REFLOG_ACTION); strbuf_release(&msg); diff --git a/reset.c b/reset.c index 5abb1a5683e..9ab007c0c34 100644 --- a/reset.c +++ b/reset.c @@ -8,7 +8,7 @@ #include "tree.h" #include "unpack-trees.h" -int reset_head(struct repository *r, struct object_id *oid, const char *action, +int reset_head(struct repository *r, struct object_id *oid, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head, const char *default_reflog_action) @@ -23,7 +23,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, struct lock_file lock = LOCK_INIT; struct unpack_trees_options unpack_tree_opts = { 0 }; struct tree *tree; - const char *reflog_action; + const char *action, *reflog_action; struct strbuf msg = STRBUF_INIT; size_t prefix_len; struct object_id *orig = NULL, oid_orig, @@ -49,6 +49,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, if (refs_only) goto reset_head_refs; + action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); unpack_tree_opts.head_idx = 1; unpack_tree_opts.src_index = r->index; diff --git a/reset.h b/reset.h index 12f83c78e28..2daec804259 100644 --- a/reset.h +++ b/reset.h @@ -12,7 +12,7 @@ #define RESET_HEAD_REFS_ONLY (1<<3) #define RESET_ORIG_HEAD (1<<4) -int reset_head(struct repository *r, struct object_id *oid, const char *action, +int reset_head(struct repository *r, struct object_id *oid, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head, const char *default_reflog_action); diff --git a/sequencer.c b/sequencer.c index 68316636921..07d2a582d39 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4130,8 +4130,7 @@ void create_autostash(struct repository *r, const char *path, path); write_file(path, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); - if (reset_head(r, NULL, "reset --hard", - NULL, RESET_HEAD_HARD, NULL, NULL, + if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL, default_reflog_action) < 0) die(_("could not reset --hard")); From patchwork Fri Oct 1 10:04:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56676C433EF for ; Fri, 1 Oct 2021 10:05:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4204761A50 for ; Fri, 1 Oct 2021 10:05:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353345AbhJAKG5 (ORCPT ); Fri, 1 Oct 2021 06:06:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353287AbhJAKGy (ORCPT ); Fri, 1 Oct 2021 06:06:54 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09835C06177E for ; Fri, 1 Oct 2021 03:05:09 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id s21so14577048wra.7 for ; Fri, 01 Oct 2021 03:05:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=9BCMdg5tfYIYrNE05cC1agH/brDhnMABkAtWHVeRW5s=; b=GFZb+UEyVsv9etlFYg4Z9GSF0QeiXqqtTsEILhkCPxItTLjcwWdmXzq2ssJlj8r2Qp btmddN3/iiLocpxdZ4XSovcwO3cBbQVP2p6cEw3/xghJ4RyuZ8yEZAszZzSLI3BbaESi oTvGwTdT0qkw92vePtjuZkyHB3aVipOHziXPzU3cc5nOyzK2SCzfymAy9/ozgs+YwMUG cC+3Kcr5uZUBosF960sjfTice0eaL+N6i5BPYGtPLPDx6Nm1lyREhTVtBb3zX6Vm99PX 7kAWHtSQ/XVALge0RsAM0XYTKDWpH4PG2y51qFNZRiUnjV5uH4YQMKLVkvGX0IoF9L4X BgCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=9BCMdg5tfYIYrNE05cC1agH/brDhnMABkAtWHVeRW5s=; b=gzhi+BVxn8EMdQdCvS0S7GGO+wMwJ3elJa/buiXEeBmlH5TM51m2cGC2sqQz5eFi52 Sw6KqujzX/mbJEzqF+zHa8tvJSSAsIBztyQywfw9Rqnn19dTO+bHHyxMdEc3uKqBUdCs yYrlzfLjzcxIP+y6WcUbwAFsfD4jzVo4VHiZbLgQxWk8EIbouIBenvV1SgpMck6I9for CItf8JTy82ySP5Ma607UVIvQWMdQQpZl0X/0PcUEf64iBAtiLFoVNia9cZwUGZil0Jd0 nyhmdWMS00ljYeNlZFSEoMPyd1v9unAhXHe3EHhoI1rZvQrsQeQMF8Wx1rR633cTNA2e Jhgw== X-Gm-Message-State: AOAM531DValgQRddzabLNvYc7rwDbW+l2l1t+0oPYwn1JAhqy0+LY2UQ MN6RHTJlX/KNWEUwlPJPVJ17R4Il3ww= X-Google-Smtp-Source: ABdhPJxyy2tU1Y2j74POVfy1BPyY7m7SHsE0A/Uyk7Pr4X5FgW8rqaMoNwdHXLUbhVOvLvWeh8IJ7g== X-Received: by 2002:adf:bbc3:: with SMTP id z3mr7851185wrg.10.1633082707432; Fri, 01 Oct 2021 03:05:07 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d1sm6258717wrr.72.2021.10.01.03.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:07 -0700 (PDT) Message-Id: <0744c3d143b5c2020267c5f9ad7da9303d7a7835.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:56 +0000 Subject: [PATCH 05/11] reset_head(): factor out ref updates Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood In the next commit we will stop trying to update HEAD when we are just clearing changes from the working tree. Move the code that updates the refs to its own function in preparation for that. Signed-off-by: Phillip Wood --- reset.c | 112 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/reset.c b/reset.c index 9ab007c0c34..668c7639127 100644 --- a/reset.c +++ b/reset.c @@ -8,26 +8,75 @@ #include "tree.h" #include "unpack-trees.h" +static int update_refs(const struct object_id *oid, const char *switch_to_branch, + const char *reflog_head, const char *reflog_orig_head, + const char *default_reflog_action, unsigned flags) +{ + unsigned detach_head = flags & RESET_HEAD_DETACH; + unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + unsigned update_orig_head = flags & RESET_ORIG_HEAD; + struct object_id *orig = NULL, oid_orig, *old_orig = NULL, oid_old_orig; + struct strbuf msg = STRBUF_INIT; + const char *reflog_action; + size_t prefix_len; + int ret; + + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); + prefix_len = msg.len; + + if (update_orig_head) { + if (!get_oid("ORIG_HEAD", &oid_old_orig)) + old_orig = &oid_old_orig; + if (!get_oid("HEAD", &oid_orig)) { + orig = &oid_orig; + if (!reflog_orig_head) { + strbuf_addstr(&msg, "updating ORIG_HEAD"); + reflog_orig_head = msg.buf; + } + update_ref(reflog_orig_head, "ORIG_HEAD", orig, + old_orig, 0, UPDATE_REFS_MSG_ON_ERR); + } else if (old_orig) + delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + } + + if (!reflog_head) { + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + reflog_head = msg.buf; + } + if (!switch_to_branch) + ret = update_ref(reflog_head, "HEAD", oid, orig, + detach_head ? REF_NO_DEREF : 0, + UPDATE_REFS_MSG_ON_ERR); + else { + ret = update_ref(reflog_head, switch_to_branch, oid, + NULL, 0, UPDATE_REFS_MSG_ON_ERR); + if (!ret) + ret = create_symref("HEAD", switch_to_branch, + reflog_head); + } + if (!ret && run_hook) + run_hook_le(NULL, "post-checkout", + oid_to_hex(orig ? orig : null_oid()), + oid_to_hex(oid), "1", NULL); + strbuf_release(&msg); + return ret; +} + int reset_head(struct repository *r, struct object_id *oid, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head, const char *default_reflog_action) { - unsigned detach_head = flags & RESET_HEAD_DETACH; unsigned reset_hard = flags & RESET_HEAD_HARD; - unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; struct object_id head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; struct unpack_trees_options unpack_tree_opts = { 0 }; struct tree *tree; - const char *action, *reflog_action; - struct strbuf msg = STRBUF_INIT; - size_t prefix_len; - struct object_id *orig = NULL, oid_orig, - *old_orig = NULL, oid_old_orig; + const char *action; int ret = 0, nr = 0; if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) @@ -47,7 +96,9 @@ int reset_head(struct repository *r, struct object_id *oid, oid = &head_oid; if (refs_only) - goto reset_head_refs; + return update_refs(oid, switch_to_branch, reflog_head, + reflog_orig_head, default_reflog_action, + flags); action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); @@ -90,49 +141,10 @@ int reset_head(struct repository *r, struct object_id *oid, goto leave_reset_head; } -reset_head_refs: - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); - prefix_len = msg.len; - - if (update_orig_head) { - if (!get_oid("ORIG_HEAD", &oid_old_orig)) - old_orig = &oid_old_orig; - if (!get_oid("HEAD", &oid_orig)) { - orig = &oid_orig; - if (!reflog_orig_head) { - strbuf_addstr(&msg, "updating ORIG_HEAD"); - reflog_orig_head = msg.buf; - } - update_ref(reflog_orig_head, "ORIG_HEAD", orig, - old_orig, 0, UPDATE_REFS_MSG_ON_ERR); - } else if (old_orig) - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); - } - - if (!reflog_head) { - strbuf_setlen(&msg, prefix_len); - strbuf_addstr(&msg, "updating HEAD"); - reflog_head = msg.buf; - } - if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, orig, - detach_head ? REF_NO_DEREF : 0, - UPDATE_REFS_MSG_ON_ERR); - else { - ret = update_ref(reflog_head, switch_to_branch, oid, - NULL, 0, UPDATE_REFS_MSG_ON_ERR); - if (!ret) - ret = create_symref("HEAD", switch_to_branch, - reflog_head); - } - if (!ret && run_hook) - run_hook_le(NULL, "post-checkout", - oid_to_hex(orig ? orig : null_oid()), - oid_to_hex(oid), "1", NULL); + ret = update_refs(oid, switch_to_branch, reflog_head, reflog_orig_head, + default_reflog_action, flags); leave_reset_head: - strbuf_release(&msg); rollback_lock_file(&lock); clear_unpack_trees_porcelain(&unpack_tree_opts); while (nr) From patchwork Fri Oct 1 10:04:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0416C433FE for ; Fri, 1 Oct 2021 10:05:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE4106134F for ; Fri, 1 Oct 2021 10:05:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353354AbhJAKG6 (ORCPT ); Fri, 1 Oct 2021 06:06:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353289AbhJAKGy (ORCPT ); Fri, 1 Oct 2021 06:06:54 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85E44C0613E2 for ; Fri, 1 Oct 2021 03:05:09 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id u18so14617507wrg.5 for ; Fri, 01 Oct 2021 03:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=0nlpRJOEtdCL+KzTLQluG+wbpMPJe/FBhyqKzosYL80=; b=otZkR36Gn0dlIcmwhuX511LxomoLJzdCdHChtR+iO8CeXXwyvY6L3t3Hp4xrEbrcFR pI5P41wKMSYBELvltcBMjImZ5ab1kXDb+DKnOPTzc2IrXAWsonngBd9jqIj+pdTsgg72 g0zzKzSO3IsVJyV9qzRn6EKbplZd8UoTPuW0o0rUwrA0biMtVbpujyTFvQ1svQ1GXmyB 4DodejiL29M79Q/Lf1he3u+1k/E0nfhD31Kjtbeg8HVKMJD8dg3rz6g4V2DierzYQjE7 WhpCUr3i242uLye+MG+57c3LzldQQJpOOBU4JKKWNLzxtg9ZYxseob/QbyDTVhGKS7DN AnJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=0nlpRJOEtdCL+KzTLQluG+wbpMPJe/FBhyqKzosYL80=; b=NK5n9YP6dFznQm2k1f9mBYep5SP/490L6hwBpvUQvRXfQXLsxUihmojhkGGyWGlO+m rEg9dFl4bc7oytwHup0w3YbjVZESUZ/rC7POI982wV+u/kuoE98ciB2+rC60vdAOBAyq QKPdaRaYsmVmBYqBRo1rchqvx0GP8WN6r5LHD0T6q0AflBlaqLyepmRlQv5nudNsQGRk +0Eh3vUwDEofRXeTVK7zP/0sPMx9cbyWi/H2mloreVleG+ITjVlBcvwMcIMjKCxQ9QKl 1uVCFnXNHX1eiNmT0oXKosDcV7FNi3sdtYN0w+ra+HrpYPfsenDDPBWSttv84XT2CM9a ADhg== X-Gm-Message-State: AOAM533yfyubLzb9tao9/YmLRL+FNrE8IYiPTcpowXNl89zjVeiMv5sK yG4yXuu3iBuWnFqznExaOPqzz2XyyY8= X-Google-Smtp-Source: ABdhPJxGLmLejxD8FLe6o3qEe4Wj99tt2MJuiGUAYhMNAw4n8LUEEgbXVKAZTXmqmJiMtN6wUCf9iA== X-Received: by 2002:adf:a3c7:: with SMTP id m7mr11208277wrb.339.1633082708083; Fri, 01 Oct 2021 03:05:08 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w17sm5156114wmi.42.2021.10.01.03.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:07 -0700 (PDT) Message-Id: <4503defe5912aecc4e6a50ac82a31aa7b230456b.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:57 +0000 Subject: [PATCH 06/11] reset_head(): make default_reflog_action optional Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood This parameter is only needed when a ref is going to be updated and the caller does not pass an explicit reflog message. Callers that are just discarding changes in the working tree like create_autostash() do not update any refs so should not have to worry about passing this parameter. Signed-off-by: Phillip Wood --- builtin/merge.c | 6 ++---- builtin/rebase.c | 14 ++++++-------- reset.c | 16 ++++++++++++---- sequencer.c | 6 +++--- sequencer.h | 3 +-- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/builtin/merge.c b/builtin/merge.c index d2c52b6e971..35833a963fc 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -1561,8 +1561,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) if (autostash) create_autostash(the_repository, - git_path_merge_autostash(the_repository), - "merge"); + git_path_merge_autostash(the_repository)); if (checkout_fast_forward(the_repository, &head_commit->object.oid, &commit->object.oid, @@ -1632,8 +1631,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) if (autostash) create_autostash(the_repository, - git_path_merge_autostash(the_repository), - "merge"); + git_path_merge_autostash(the_repository)); /* We are going to make a new commit. */ git_committer_info(IDENT_STRICT); diff --git a/builtin/rebase.c b/builtin/rebase.c index 26f53c55cc4..1a6af508f49 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -791,8 +791,7 @@ static int move_to_original_branch(struct rebase_options *opts) opts->head_name); ret = reset_head(the_repository, NULL, opts->head_name, RESET_HEAD_REFS_ONLY, - orig_head_reflog.buf, head_reflog.buf, - DEFAULT_REFLOG_ACTION); + orig_head_reflog.buf, head_reflog.buf, NULL); strbuf_release(&orig_head_reflog); strbuf_release(&head_reflog); @@ -1109,7 +1108,7 @@ static int checkout_up_to_date(struct rebase_options *options) options->switch_to); if (reset_head(the_repository, &options->orig_head, options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK, - NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0) + NULL, buf.buf, NULL) < 0) ret = error(_("could not switch to %s"), options->switch_to); strbuf_release(&buf); @@ -1558,7 +1557,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) string_list_clear(&merge_rr, 1); if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD, - NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) + NULL, NULL, NULL) < 0) die(_("could not discard worktree changes")); remove_branch_state(the_repository, 0); if (read_basic_state(&options)) @@ -1964,8 +1963,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("could not read index")); if (options.autostash) { - create_autostash(the_repository, state_dir_path("autostash", &options), - DEFAULT_REFLOG_ACTION); + create_autostash(the_repository, + state_dir_path("autostash", &options)); } if (require_clean_work_tree(the_repository, "rebase", @@ -2083,8 +2082,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.head_name ? options.head_name : "detached HEAD", oid_to_hex(&options.onto->object.oid)); reset_head(the_repository, NULL, options.head_name, - RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, - DEFAULT_REFLOG_ACTION); + RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, NULL); strbuf_release(&msg); ret = finish_rebase(&options); goto cleanup; diff --git a/reset.c b/reset.c index 668c7639127..be4e18eed98 100644 --- a/reset.c +++ b/reset.c @@ -21,8 +21,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch size_t prefix_len; int ret; - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); + if ((update_orig_head && !reflog_orig_head) || !reflog_head) { + if (!default_reflog_action) + BUG("default_reflog_action must be given when reflog messages are omitted"); + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : + default_reflog_action); + } prefix_len = msg.len; if (update_orig_head) { @@ -71,6 +76,7 @@ int reset_head(struct repository *r, struct object_id *oid, { unsigned reset_hard = flags & RESET_HEAD_HARD; unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; + unsigned update_orig_head = flags & RESET_ORIG_HEAD; struct object_id head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; @@ -141,8 +147,10 @@ int reset_head(struct repository *r, struct object_id *oid, goto leave_reset_head; } - ret = update_refs(oid, switch_to_branch, reflog_head, reflog_orig_head, - default_reflog_action, flags); + if (oid != &head_oid || update_orig_head || switch_to_branch) + ret = update_refs(oid, switch_to_branch, reflog_head, + reflog_orig_head, default_reflog_action, + flags); leave_reset_head: rollback_lock_file(&lock); diff --git a/sequencer.c b/sequencer.c index 07d2a582d39..56a204f18c5 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4093,8 +4093,8 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset) return -1; } -void create_autostash(struct repository *r, const char *path, - const char *default_reflog_action) +void create_autostash(struct repository *r, const char *path) + { struct strbuf buf = STRBUF_INIT; struct lock_file lock_file = LOCK_INIT; @@ -4131,7 +4131,7 @@ void create_autostash(struct repository *r, const char *path, write_file(path, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL, - default_reflog_action) < 0) + NULL) < 0) die(_("could not reset --hard")); if (discard_index(r->index) < 0 || diff --git a/sequencer.h b/sequencer.h index 2088344cb37..92357536ff4 100644 --- a/sequencer.h +++ b/sequencer.h @@ -198,8 +198,7 @@ void commit_post_rewrite(struct repository *r, const struct commit *current_head, const struct object_id *new_head); -void create_autostash(struct repository *r, const char *path, - const char *default_reflog_action); +void create_autostash(struct repository *r, const char *path); int save_autostash(const char *path); int apply_autostash(const char *path); int apply_autostash_oid(const char *stash_oid); From patchwork Fri Oct 1 10:04:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530123 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E4ECC433F5 for ; Fri, 1 Oct 2021 10:07:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6C5A6134F for ; Fri, 1 Oct 2021 10:07:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229990AbhJAKHK (ORCPT ); Fri, 1 Oct 2021 06:07:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353348AbhJAKG5 (ORCPT ); Fri, 1 Oct 2021 06:06:57 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BD6AC06177E for ; Fri, 1 Oct 2021 03:05:13 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id j27so6839504wms.0 for ; Fri, 01 Oct 2021 03:05:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=3kTtfa/DmY9ZtEc0Lvb3lNHSoshMcsAlDzPm3Fhv4a4=; b=cuV66aC3qcyRAKfaYzAN+AiugWs1a86JbY+T8XyNZfWLQPtiNHly85bDLdaFwpGq4K GxQxw6T6jlceBPnDvtUGrFI4DcGh/aY8Ys+KdX9sFj5NBD8sibkeRMW0JaYMdB1hTiiy vE8gYkwc5DnwOBo4UPptFtClE1907F+HGUS9qAIcgAX8giORFkpBg8FTiihzhEYftlyz AnRUehPxwfGVvTRN31Fed7b5B0EIsQvMqbn3Vlf5vGf3PF+rp+8zD5xYVwYAxXOlb9Oh ZLPOrw76bK8esGOWhR12JoT5oyDfge5C6En9vCJyfrLBcntoFpX5HbOMCTsFHrtoQyKA d4Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=3kTtfa/DmY9ZtEc0Lvb3lNHSoshMcsAlDzPm3Fhv4a4=; b=JlkyRdrtfQQNAxTDahoiFdZbbjHlPDUicON/mAwub5pYAwGlpDqjqEWBbpVoxc62PN WorTzutw/I5Pdx8s4Hm6VT7t5e5VNvcFb0MW/nbYiutS+JmqlaUW61aqauE/N+1gAuTm g2OljSO023Xi/A5/1CnLzpcSAfulVIBt4L3Zm2X9J7VLJvvQ64K/NctbpchwZ4xtE2RH JK2OMy+gwIyWdZ04Dxb3T9zDPvdBY0z2bpKbLmQMH2sFsrQ0xmzd29EmfjEnB8w4+nh2 4EKIPwco2TUd+JT/4i2TwlCreRoN3d7mpMekpCFVChUe2M4tvxEWZg7uUzSpKPXakFf7 WpjA== X-Gm-Message-State: AOAM5331hLk4h96Hks6ZmM42cd7WGewUd06JunXwDDZ77wobjTettL1b 5x24zKZPYHnE0fpNIt6NvzJhUrsCVbc= X-Google-Smtp-Source: ABdhPJylGrGr7GIQUyIaUbhSS5J9r09Zu0qnyVM8g3rubaBipewJlPA0kk5qiSm6ZtV3RZR7gKaQlQ== X-Received: by 2002:a05:600c:350f:: with SMTP id h15mr3460727wmq.123.1633082708679; Fri, 01 Oct 2021 03:05:08 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z17sm5379633wrr.49.2021.10.01.03.05.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:08 -0700 (PDT) Message-Id: <5ffc7e64ff1cb306dc96abf3b0fa65c0662994d8.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:58 +0000 Subject: [PATCH 07/11] rebase: cleanup reset_head() calls Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood If ORIG_HEAD is not set by passing RESET_ORIG_HEAD then there is no need to pass anything for reflog_orig_head. In addition to the callers fixed in this commit move_to_original_branch() also passes reflog_orig_head without setting ORIG_HEAD. That caller is mistakenly passing the message it wants to put in the branch reflog which is not currently possible so we delay fixing that caller until we can pass the message as the branch reflog. Signed-off-by: Phillip Wood --- builtin/rebase.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 1a6af508f49..89b348ffa75 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -881,7 +881,7 @@ static int run_am(struct rebase_options *opts) reset_head(the_repository, &opts->orig_head, opts->head_name, 0, - "HEAD", NULL, DEFAULT_REFLOG_ACTION); + NULL, NULL, DEFAULT_REFLOG_ACTION); error(_("\ngit encountered an error while preparing the " "patches to replay\n" "these revisions:\n" @@ -2082,7 +2082,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) options.head_name ? options.head_name : "detached HEAD", oid_to_hex(&options.onto->object.oid)); reset_head(the_repository, NULL, options.head_name, - RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, NULL); + RESET_HEAD_REFS_ONLY, NULL, msg.buf, NULL); strbuf_release(&msg); ret = finish_rebase(&options); goto cleanup; From patchwork Fri Oct 1 10:04:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530115 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93BFFC433F5 for ; Fri, 1 Oct 2021 10:05:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7CC626134F for ; Fri, 1 Oct 2021 10:05:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353319AbhJAKHB (ORCPT ); Fri, 1 Oct 2021 06:07:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353290AbhJAKGz (ORCPT ); Fri, 1 Oct 2021 06:06:55 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B46BC06177B for ; Fri, 1 Oct 2021 03:05:11 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id k7so14486734wrd.13 for ; Fri, 01 Oct 2021 03:05:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=jIRqKcH2tEa0UjuI9KfVPTfvk6Wug3iny0OUNyQINsM=; b=oNV28EPh0vL8iv4nFFUnWA+qAeTqEi0kTnYuPldP1UfaqRVZ11H6UEgBA6JLBNOUju epkBHQ5RGiPMLbk59uaK0LOqlAbhrxbgMrx7PTTgH47F0Pr68ABIejy4FoCsyTWOlK8f 3d6rm1D/kvrq0rcxLQNqBqLwJBNJeHacAw1ytGYlb+Y1a9xyp7hSToNdekiXwrPcRKgK NStCw7z9LZGnWodvoN60CUYjS2ptUrAppcRTyIhfPSBXPY4ZleOw1ZH0+wJsDfN6dByM zfxonL94QCeZmdOwg7cP2dQL59J/3A7OU0DecJFYkV4/fv1w4PdABQfI1K+/TlUt/tjr izCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=jIRqKcH2tEa0UjuI9KfVPTfvk6Wug3iny0OUNyQINsM=; b=GgO+lqSVhK9vLEJ3jYjYdtgHkL1OtIQc/+FApX9aBDW2FgT0X/kfZ4sJ1S6JhkMYAQ VmG1ONyT6/vTrXduaFJUcUz2LBFOGUbGqty6oLky0HoAkVRfk9NLcvtJU2oQTfopm7vd 29adEPMugmNsiXgkLwurrE7ZXv4V5DQq963O+JZ4IjPawdm5oW24Zh8xd3eqWFyU6PaX iqBSTpTTDibA41PLfLIenRLRGclX4iDCq5lb9U3SuqXId+jXIhGWHIe0v3sqA7Xq9MyX deyMg/tIWu/nePFeTbe5X5eWLVdVhEMD7ATtP6sScaiw5zxmIM5Ugy4/0nYRpkdbRcA+ 5jqQ== X-Gm-Message-State: AOAM530O8PMR6tbyqstLqhOvaL0brHG+wvl3vbeSqvhVehfHP9d1Z46K tIgG8YJQpsQCXHei50QOdB+4pH8RjHw= X-Google-Smtp-Source: ABdhPJxeMuDBjbvKncIoIzwMBkj716cotSGhdanBzYxVvDRdyLE8V3jpC2u/0jscB1G0Eag8fZxg0g== X-Received: by 2002:a5d:6a8f:: with SMTP id s15mr11353043wru.15.1633082709288; Fri, 01 Oct 2021 03:05:09 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a8sm5457832wrp.61.2021.10.01.03.05.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:09 -0700 (PDT) Message-Id: <267e074e6dbd3c7cf733cd5ba4fa476d2d5e6fd2.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:04:59 +0000 Subject: [PATCH 08/11] reset_head(): take struct rebase_head_opts Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood This function already takes a confusingly large number of parameters some of which are optional or not always required. The following commits will add a couple more parameters so change it to take a struct of options first. Signed-off-by: Phillip Wood --- builtin/rebase.c | 57 ++++++++++++++++++++++++++++++------------------ reset.c | 36 +++++++++++++++--------------- reset.h | 29 ++++++++++++++++++++---- sequencer.c | 5 ++--- 4 files changed, 80 insertions(+), 47 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 89b348ffa75..ae5de271e9a 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -777,6 +777,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value) static int move_to_original_branch(struct rebase_options *opts) { struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; + struct reset_head_opts ropts = { 0 }; int ret; if (!opts->head_name) @@ -789,9 +790,11 @@ static int move_to_original_branch(struct rebase_options *opts) opts->head_name, oid_to_hex(&opts->onto->object.oid)); strbuf_addf(&head_reflog, "rebase finished: returning to %s", opts->head_name); - ret = reset_head(the_repository, NULL, opts->head_name, - RESET_HEAD_REFS_ONLY, - orig_head_reflog.buf, head_reflog.buf, NULL); + ropts.branch = opts->head_name; + ropts.flags = RESET_HEAD_REFS_ONLY; + ropts.orig_head_msg = orig_head_reflog.buf; + ropts.head_msg = head_reflog.buf; + ret = reset_head(the_repository, &ropts); strbuf_release(&orig_head_reflog); strbuf_release(&head_reflog); @@ -875,13 +878,15 @@ static int run_am(struct rebase_options *opts) status = run_command(&format_patch); if (status) { + struct reset_head_opts ropts = { 0 }; unlink(rebased_patches); free(rebased_patches); strvec_clear(&am.args); - reset_head(the_repository, &opts->orig_head, - opts->head_name, 0, - NULL, NULL, DEFAULT_REFLOG_ACTION); + ropts.oid = &opts->orig_head; + ropts.branch = opts->head_name; + ropts.default_reflog_action = DEFAULT_REFLOG_ACTION; + reset_head(the_repository, &ropts); error(_("\ngit encountered an error while preparing the " "patches to replay\n" "these revisions:\n" @@ -1101,14 +1106,17 @@ static int rebase_config(const char *var, const char *value, void *data) static int checkout_up_to_date(struct rebase_options *options) { struct strbuf buf = STRBUF_INIT; + struct reset_head_opts ropts = { 0 }; int ret = 0; strbuf_addf(&buf, "%s: checkout %s", getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options->switch_to); - if (reset_head(the_repository, &options->orig_head, - options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK, - NULL, buf.buf, NULL) < 0) + ropts.oid = &options->orig_head; + ropts.branch = options->head_name; + ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + ropts.head_msg = buf.buf; + if (reset_head(the_repository, &ropts) < 0) ret = error(_("could not switch to %s"), options->switch_to); strbuf_release(&buf); @@ -1319,6 +1327,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) char *squash_onto_name = NULL; int reschedule_failed_exec = -1; int allow_preemptive_ff = 1; + struct reset_head_opts ropts = { 0 }; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -1555,9 +1564,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) rerere_clear(the_repository, &merge_rr); string_list_clear(&merge_rr, 1); - - if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD, - NULL, NULL, NULL) < 0) + ropts.flags = RESET_HEAD_HARD; + if (reset_head(the_repository, &ropts) < 0) die(_("could not discard worktree changes")); remove_branch_state(the_repository, 0); if (read_basic_state(&options)) @@ -1574,9 +1582,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (read_basic_state(&options)) exit(1); - if (reset_head(the_repository, &options.orig_head, - options.head_name, RESET_HEAD_HARD, - NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) + ropts.oid = &options.orig_head; + ropts.branch = options.head_name; + ropts.flags = RESET_HEAD_HARD; + ropts.default_reflog_action = DEFAULT_REFLOG_ACTION; + if (reset_head(the_repository, &ropts) < 0) die(_("could not move back to %s"), oid_to_hex(&options.orig_head)); remove_branch_state(the_repository, 0); @@ -2063,10 +2073,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&msg, "%s: checkout %s", getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); - if (reset_head(the_repository, &options.onto->object.oid, NULL, - RESET_HEAD_DETACH | RESET_ORIG_HEAD | - RESET_HEAD_RUN_POST_CHECKOUT_HOOK, - NULL, msg.buf, DEFAULT_REFLOG_ACTION)) + ropts.oid = &options.onto->object.oid; + ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | + RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + ropts.head_msg = msg.buf; + ropts.default_reflog_action = DEFAULT_REFLOG_ACTION; + if (reset_head(the_repository, &ropts)) die(_("Could not detach HEAD")); strbuf_release(&msg); @@ -2081,8 +2093,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&msg, "rebase finished: %s onto %s", options.head_name ? options.head_name : "detached HEAD", oid_to_hex(&options.onto->object.oid)); - reset_head(the_repository, NULL, options.head_name, - RESET_HEAD_REFS_ONLY, NULL, msg.buf, NULL); + memset(&ropts, 0, sizeof(ropts)); + ropts.branch = options.head_name; + ropts.flags = RESET_HEAD_REFS_ONLY; + ropts.head_msg = msg.buf; + reset_head(the_repository, &ropts); strbuf_release(&msg); ret = finish_rebase(&options); goto cleanup; diff --git a/reset.c b/reset.c index be4e18eed98..e77a8ac423d 100644 --- a/reset.c +++ b/reset.c @@ -8,13 +8,16 @@ #include "tree.h" #include "unpack-trees.h" -static int update_refs(const struct object_id *oid, const char *switch_to_branch, - const char *reflog_head, const char *reflog_orig_head, - const char *default_reflog_action, unsigned flags) +static int update_refs(const struct reset_head_opts *opts, + const struct object_id *oid) { - unsigned detach_head = flags & RESET_HEAD_DETACH; - unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; + unsigned detach_head = opts->flags & RESET_HEAD_DETACH; + unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; + const char *switch_to_branch = opts->branch; + const char *reflog_head = opts->head_msg; + const char *reflog_orig_head = opts->orig_head_msg; + const char *default_reflog_action = opts->default_reflog_action; struct object_id *orig = NULL, oid_orig, *old_orig = NULL, oid_old_orig; struct strbuf msg = STRBUF_INIT; const char *reflog_action; @@ -69,14 +72,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch return ret; } -int reset_head(struct repository *r, struct object_id *oid, - const char *switch_to_branch, unsigned flags, - const char *reflog_orig_head, const char *reflog_head, - const char *default_reflog_action) +int reset_head(struct repository *r, const struct reset_head_opts *opts) { - unsigned reset_hard = flags & RESET_HEAD_HARD; - unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; + const struct object_id *oid = opts->oid; + const char *switch_to_branch = opts->branch; + unsigned reset_hard = opts->flags & RESET_HEAD_HARD; + unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY; + unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; struct object_id head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; @@ -102,9 +104,7 @@ int reset_head(struct repository *r, struct object_id *oid, oid = &head_oid; if (refs_only) - return update_refs(oid, switch_to_branch, reflog_head, - reflog_orig_head, default_reflog_action, - flags); + return update_refs(opts, oid); action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); @@ -148,9 +148,7 @@ int reset_head(struct repository *r, struct object_id *oid, } if (oid != &head_oid || update_orig_head || switch_to_branch) - ret = update_refs(oid, switch_to_branch, reflog_head, - reflog_orig_head, default_reflog_action, - flags); + ret = update_refs(opts, oid); leave_reset_head: rollback_lock_file(&lock); diff --git a/reset.h b/reset.h index 2daec804259..996d7f569b6 100644 --- a/reset.h +++ b/reset.h @@ -12,9 +12,30 @@ #define RESET_HEAD_REFS_ONLY (1<<3) #define RESET_ORIG_HEAD (1<<4) -int reset_head(struct repository *r, struct object_id *oid, - const char *switch_to_branch, unsigned flags, - const char *reflog_orig_head, const char *reflog_head, - const char *default_reflog_action); +struct reset_head_opts { + /* The oid of the commit to checkout/reset to. Defaults to HEAD */ + const struct object_id *oid; + /* Optional branch to switch to */ + const char *branch; + /* Flags defined above */ + unsigned flags; + /* + * Optional reflog message for HEAD, if this is not set then + * default_reflog_action must be. + */ + const char *head_msg; + /* + * Optional reflog message for ORIG_HEAD, if this is not set and flags + * contains RESET_ORIG_HEAD then default_reflog_action must be set. + */ + const char *orig_head_msg; + /* + * Action to use in default reflog messages, only required if a ref is + * being updated and the reflog messages above are omitted. + */ + const char *default_reflog_action; +}; + +int reset_head(struct repository *r, const struct reset_head_opts *opts); #endif diff --git a/sequencer.c b/sequencer.c index 56a204f18c5..36ab6854c6b 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4109,6 +4109,7 @@ void create_autostash(struct repository *r, const char *path) if (has_unstaged_changes(r, 1) || has_uncommitted_changes(r, 1)) { struct child_process stash = CHILD_PROCESS_INIT; + struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD }; struct object_id oid; strvec_pushl(&stash.args, @@ -4130,10 +4131,8 @@ void create_autostash(struct repository *r, const char *path) path); write_file(path, "%s", oid_to_hex(&oid)); printf(_("Created autostash: %s\n"), buf.buf); - if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL, - NULL) < 0) + if (reset_head(r, &ropts) < 0) die(_("could not reset --hard")); - if (discard_index(r->index) < 0 || repo_read_index(r) < 0) die(_("could not read index")); From patchwork Fri Oct 1 10:05:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9C42C433EF for ; Fri, 1 Oct 2021 10:05:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B1BD66134F for ; Fri, 1 Oct 2021 10:05:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353381AbhJAKHA (ORCPT ); Fri, 1 Oct 2021 06:07:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353305AbhJAKGz (ORCPT ); Fri, 1 Oct 2021 06:06:55 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41ED6C06177C for ; Fri, 1 Oct 2021 03:05:11 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id u18so14617640wrg.5 for ; Fri, 01 Oct 2021 03:05:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=VcfVJ4dV5con/lrNmc4N1kdzm1Xh75SKejjks+zK+lw=; b=QzIXQgmNTprvCm96UQVunfjEylqQ5v3liHMomXfXn9ZyBXspQoM31NdWeXSSCtyI+P xwf6kRGQHALrnU7TrD5Nwc/plkb3juw+mcsXa7DWGh28EWJktJSDMjTyB4ITEU2mEddI 0L/9N/nDHHo6SFrnHpnmVtG5TOPmMBBn6uzG1d7/gMyE7zRwmGEHY6iQaIJBeujDtQBD r0eQmV3zYfipV3uEulKInItuTJIFgHcdyg6e+t95bHiJdOfZySEGDQCRpu+9z4Sk2klO Qr+xzPJt3QcBlVKa2BwCcUpdoKLmMlHRDVQ1ZWCJrDU25K36SmLzgW7jxsJrUCgzfEYu dUgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=VcfVJ4dV5con/lrNmc4N1kdzm1Xh75SKejjks+zK+lw=; b=NQZnPW/zwqVoALsNSxu9+G4eZYCzi8QaH9YoGsWvu9mwVi5IGY2YLbgZuY258WA3rm 7Dn4o9N1uESPw6x6apGICMQDtobGlWzc7ousFCw9Q30KivC2kdavrKf1j9H59bjUTzM4 MnVqxW47GvjW57/3FwIyuc6CXEypV7caFP7y7v5pwCcOnTPjKH2c9jqJ9zgJvYSLQjDi tII2rPpMya4DO1jRZO0hy8TqXAkDNYUzD8U9J6AF3SVW/7W07usClasMJYOtO6awTado E439jl9k6/iCuJG6SWoXmtWmBpgCasY9Xz6T7HAN+XG933+QW5yTjp7cOh4vZIS8j5s0 zxJQ== X-Gm-Message-State: AOAM5337CMUzngkOLCi4MORDFjOPwLlIi99FqXYSZgxItaTDd+ikTJ6g YiyGgm4B2ln+lbadlHplpNvFS5nM5sY= X-Google-Smtp-Source: ABdhPJyTv1+FstI3urzRv7GxIjDRgXtBdvtUy8LYIpfRGSN1sFCaVBdjXQOfkJFNuKW6UyENGeWTVg== X-Received: by 2002:a5d:6d03:: with SMTP id e3mr11111920wrq.383.1633082709844; Fri, 01 Oct 2021 03:05:09 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n15sm5443550wrg.58.2021.10.01.03.05.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:09 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 01 Oct 2021 10:05:00 +0000 Subject: [PATCH 09/11] rebase --apply: fix reflog Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood move_to_original_branch() passes the message intended for the branch reflog as `orig_head_msg`. Fix this by adding a `branch_msg` member to struct reset_head_opts and add a regression test. Note that these reflog messages do not respect GIT_REFLOG_ACTION. They are not alone in that and will be fixed in a future series. The "merge" backend already has tests that check both the branch and HEAD reflogs. Signed-off-by: Phillip Wood --- builtin/rebase.c | 8 ++++---- reset.c | 12 ++++++++++-- reset.h | 2 ++ t/t3406-rebase-message.sh | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index ae5de271e9a..c40a8b843f4 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -776,7 +776,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value) static int move_to_original_branch(struct rebase_options *opts) { - struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; + struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; struct reset_head_opts ropts = { 0 }; int ret; @@ -786,17 +786,17 @@ static int move_to_original_branch(struct rebase_options *opts) if (!opts->onto) BUG("move_to_original_branch without onto"); - strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s", + strbuf_addf(&branch_reflog, "rebase finished: %s onto %s", opts->head_name, oid_to_hex(&opts->onto->object.oid)); strbuf_addf(&head_reflog, "rebase finished: returning to %s", opts->head_name); ropts.branch = opts->head_name; ropts.flags = RESET_HEAD_REFS_ONLY; - ropts.orig_head_msg = orig_head_reflog.buf; + ropts.branch_msg = branch_reflog.buf; ropts.head_msg = head_reflog.buf; ret = reset_head(the_repository, &ropts); - strbuf_release(&orig_head_reflog); + strbuf_release(&branch_reflog); strbuf_release(&head_reflog); return ret; } diff --git a/reset.c b/reset.c index e77a8ac423d..2c32600234d 100644 --- a/reset.c +++ b/reset.c @@ -15,6 +15,7 @@ static int update_refs(const struct reset_head_opts *opts, unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; const char *switch_to_branch = opts->branch; + const char *reflog_branch = opts->branch_msg; const char *reflog_head = opts->head_msg; const char *reflog_orig_head = opts->orig_head_msg; const char *default_reflog_action = opts->default_reflog_action; @@ -58,8 +59,9 @@ static int update_refs(const struct reset_head_opts *opts, detach_head ? REF_NO_DEREF : 0, UPDATE_REFS_MSG_ON_ERR); else { - ret = update_ref(reflog_head, switch_to_branch, oid, - NULL, 0, UPDATE_REFS_MSG_ON_ERR); + ret = update_ref(reflog_branch ? reflog_branch : reflog_head, + switch_to_branch, oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR); if (!ret) ret = create_symref("HEAD", switch_to_branch, reflog_head); @@ -90,6 +92,12 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts) if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) BUG("Not a fully qualified branch: '%s'", switch_to_branch); + if (opts->orig_head_msg && !update_orig_head) + BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD"); + + if (opts->branch_msg && !opts->branch) + BUG("branch reflog message given without a branch"); + if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { ret = -1; goto leave_reset_head; diff --git a/reset.h b/reset.h index 996d7f569b6..c0f4e99a3be 100644 --- a/reset.h +++ b/reset.h @@ -19,6 +19,8 @@ struct reset_head_opts { const char *branch; /* Flags defined above */ unsigned flags; + /* Optional reflog message for branch, defaults to head_msg. */ + const char *branch_msg; /* * Optional reflog message for HEAD, if this is not set then * default_reflog_action must be. diff --git a/t/t3406-rebase-message.sh b/t/t3406-rebase-message.sh index 77a313f62eb..d17b450e811 100755 --- a/t/t3406-rebase-message.sh +++ b/t/t3406-rebase-message.sh @@ -105,6 +105,29 @@ test_expect_success 'GIT_REFLOG_ACTION' ' test_cmp expect actual ' +test_expect_success 'rebase --apply reflog' ' + git checkout -b reflog-apply start && + old_head_reflog="$(git log -g --format=%gs -1 HEAD)" && + + git rebase --apply Y && + + git log -g --format=%gs -4 HEAD >actual && + cat >expect <<-EOF && + rebase finished: returning to refs/heads/reflog-apply + rebase: Z + rebase: checkout Y + $old_head_reflog + EOF + test_cmp expect actual && + + git log -g --format=%gs -2 reflog-apply >actual && + cat >expect <<-EOF && + rebase finished: refs/heads/reflog-apply onto $(git rev-parse Y) + branch: Created from start + EOF + test_cmp expect actual +' + test_expect_success 'rebase -i onto unrelated history' ' git init unrelated && test_commit -C unrelated 1 && From patchwork Fri Oct 1 10:05:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D8A4C433EF for ; Fri, 1 Oct 2021 10:05:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A87A61A50 for ; Fri, 1 Oct 2021 10:05:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353350AbhJAKHC (ORCPT ); Fri, 1 Oct 2021 06:07:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353310AbhJAKG4 (ORCPT ); Fri, 1 Oct 2021 06:06:56 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAF96C06177E for ; Fri, 1 Oct 2021 03:05:11 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id k7so14486783wrd.13 for ; Fri, 01 Oct 2021 03:05:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=eXgGy+LpkqhTOu6wddtYEhn81YJtBT0sMRG81TaMZyA=; b=fC6BGYGolugqoNnWtefpqqKgGZ9s0UeVhKGMTlKmF0BIVEzacvfa8fgP21UP6KZXX1 Q8edHXjJGRRDw4TAn7A55ZVAI4k973G6QprJB4nkUckqXtc4MgpbPKW/vYc1+Q6w24NB 7ckVCOTm5BwM/viqtMz+EsBj/XPKBtgqDspdQ0ANUs/+oq/5A4yWRJIZT3hnaTX1V7Nd JOF8EuWM1fYDzmh2wr+Sp4vN3nrVW3vCJlGK7jBgK5bKP0ttbD0aEoj0ZE9v67bPe050 Q+e5x1k/b7Ef0uVphTMDBOvGhFzbvloUWIcSBEc4hzOTnMY6tN8tDVb8Ji9F6RNCZytu pFAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=eXgGy+LpkqhTOu6wddtYEhn81YJtBT0sMRG81TaMZyA=; b=hSAbfW2053Ij3eQ5mL0cCkhPbrtAy1OGclDgiLttR9QOzyTK2rVjad67jE3QLRoRiw MQmhTmNxzu/350vfpZA/uooL11Rxv6H49hgddRHszbx/DXc+JIKE3q/LyeM5NxmS8L71 Gb5HRuh/iMzhLUdd0NVVZSC1roDr174FgFps+hGPrkW1ka/PqQchQwxvFuwFt9GhZRZX KcyiAxXNb3bMLWh8BNTOb41rHSF3UWnaTHUWOV0M7FaOD+bFzwUaVa/1vxHT3fWUJNFS i/Mz1VBoMS/JUhNxWVsw68ivZFHdeL1xR+TXwxGu2q/9g9LTDXAnDQd/2hsyqwAgwNsA yiRg== X-Gm-Message-State: AOAM533Qil2paaLPBm8KHqjIqSrqYbzvbdPa/znuDc2D/7pwWtGduGR+ QXkeqr54R/bpCdwVpKWbu6U9hbJdjo0= X-Google-Smtp-Source: ABdhPJwg4iHZITzS9kbt/jayzGtmZgUQGPeRCZoNFhJG+WzZmHrnWG5SYH7F+UiF3PwC4P9NIh9tug== X-Received: by 2002:adf:f0d2:: with SMTP id x18mr11713720wro.25.1633082710413; Fri, 01 Oct 2021 03:05:10 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l13sm5282049wme.38.2021.10.01.03.05.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:10 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 01 Oct 2021 10:05:01 +0000 Subject: [PATCH 10/11] rebase --apply: set ORIG_HEAD correctly Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood At the start of a rebase ORIG_HEAD is updated to tip of the branch being rebased. Unfortunately reset_head() always uses the current value of HEAD for this which is incorrect if the rebase is started with 'git rebase ' as in that case ORIG_HEAD should be updated to . This only affects the "apply" backend as the "merge" backend does not yet use reset_head() for the initial checkout. Fix this by passing in orig_head when calling reset_head() and add some regression tests. Signed-off-by: Phillip Wood --- builtin/rebase.c | 1 + reset.c | 4 +++- reset.h | 2 ++ t/t3418-rebase-continue.sh | 26 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index c40a8b843f4..c36a8a10e9b 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -2074,6 +2074,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_addf(&msg, "%s: checkout %s", getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); ropts.oid = &options.onto->object.oid; + ropts.orig_head = &options.orig_head, ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | RESET_HEAD_RUN_POST_CHECKOUT_HOOK; ropts.head_msg = msg.buf; diff --git a/reset.c b/reset.c index 2c32600234d..82bcf2dcb8b 100644 --- a/reset.c +++ b/reset.c @@ -14,6 +14,7 @@ static int update_refs(const struct reset_head_opts *opts, unsigned detach_head = opts->flags & RESET_HEAD_DETACH; unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; + const struct object_id *orig_head = opts->orig_head; const char *switch_to_branch = opts->branch; const char *reflog_branch = opts->branch_msg; const char *reflog_head = opts->head_msg; @@ -43,7 +44,8 @@ static int update_refs(const struct reset_head_opts *opts, strbuf_addstr(&msg, "updating ORIG_HEAD"); reflog_orig_head = msg.buf; } - update_ref(reflog_orig_head, "ORIG_HEAD", orig, + update_ref(reflog_orig_head, "ORIG_HEAD", + orig_head ? orig_head : orig, old_orig, 0, UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) delete_ref(NULL, "ORIG_HEAD", old_orig, 0); diff --git a/reset.h b/reset.h index c0f4e99a3be..91a868c5ba8 100644 --- a/reset.h +++ b/reset.h @@ -15,6 +15,8 @@ struct reset_head_opts { /* The oid of the commit to checkout/reset to. Defaults to HEAD */ const struct object_id *oid; + /* Optional commit when setting ORIG_HEAD. Defaults to HEAD */ + const struct object_id *orig_head; /* Optional branch to switch to */ const char *branch; /* Flags defined above */ diff --git a/t/t3418-rebase-continue.sh b/t/t3418-rebase-continue.sh index 738fbae9b29..be63456c5b9 100755 --- a/t/t3418-rebase-continue.sh +++ b/t/t3418-rebase-continue.sh @@ -323,4 +323,30 @@ test_expect_success 'there is no --no-reschedule-failed-exec in an ongoing rebas test_expect_code 129 git rebase --edit-todo --no-reschedule-failed-exec ' +test_orig_head_helper() { + test_when_finished 'git rebase --abort && + git checkout topic && + git reset --hard commit-new-file-F2-on-topic-branch' && + git update-ref -d ORIG_HEAD && + test_must_fail git rebase "$@" && + test_cmp_rev ORIG_HEAD commit-new-file-F2-on-topic-branch +} + +test_orig_head() { + type=$1 + test_expect_success "rebase $type sets ORIG_HEAD correctly" ' + git checkout topic && + git reset --hard commit-new-file-F2-on-topic-branch && + test_orig_head_helper $type main + ' + + test_expect_success "rebase $type sets ORIG_HEAD correctly" ' + git checkout main && + test_orig_head_helper $type main topic + ' +} + +test_orig_head --apply +test_orig_head --merge + test_done From patchwork Fri Oct 1 10:05:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12530121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F480C433EF for ; Fri, 1 Oct 2021 10:05:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 78E076134F for ; Fri, 1 Oct 2021 10:05:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353416AbhJAKHH (ORCPT ); Fri, 1 Oct 2021 06:07:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353273AbhJAKG4 (ORCPT ); Fri, 1 Oct 2021 06:06:56 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ECA4C06177B for ; Fri, 1 Oct 2021 03:05:12 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id k7so14486825wrd.13 for ; Fri, 01 Oct 2021 03:05:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=c1Irhq8IsPa+LL7gETWc+PQ//HgECZnjwhNdRzGczCg=; b=KJLucpcX+DeN4AwF24Pv+LDaqiMGjr5G4vo6TbVtTBs7zmsQazdMKZCnp0+6o8pyOF oZtYB4A6554mzRN4WYQxUKtHoTaScyBxJr5ZRIFxgrgOScxhy5fnnGACRV4pY1Y14KOs 0cE1ar55hoiS7uF73FXtzRCjwJbxx/CDNJUyA39gQb2vzjLZ/cn77vi+39C5qi7kETt5 STYq39LRjzwXtWuwGnHa6NZEJ+tD/wWSJL1Becqlq6/QX9qtjDkxvx7JON5pmkh0govC j7xXy/4xqn5Cwgy5IM7wYWuRDJORDl1JVAdFe469YHbmmFb9KRpdeiTcP8bcac2KseYb +hzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=c1Irhq8IsPa+LL7gETWc+PQ//HgECZnjwhNdRzGczCg=; b=jFB5oevpMBUpXFc9hhHNiinxVAeZCSVKQAECQsMRBWB6ILukFd+o6VRJXVEloYPoba WlQEv0IADLvDDz4n4Ov2C0DCk0NMkpEB9iUMeA4qgZinypxKf950gsAYmc1BfKFXzjgm x8pY4EKPq+0pnnvmK3xGmqRTXDu7OyzLfzJZOgeJXG1QEVv4XfRtFfYGBMDlDDXb7oUw Jk5zRn5bUY142icsrnPu+w7UEjWNJMCFMFEC8i0aw7leTSPiAhmdpPq+gNh5J/W34sLO P4z1ySLmxZfrf1qrwN1mn90nNFOrbkfp0Fi0VWaid4Kw5jj5dJjKsyiwmHcwa7/h/V2a oZmw== X-Gm-Message-State: AOAM53025t3L2ZA1MGo7+9tkmqsKrHkQUYpFv9ISm0IzTwcyC9WRorpZ F5y77yh1Nf37Lma1aAe1pZfkD/WBeqM= X-Google-Smtp-Source: ABdhPJzUq3mSqLbau+aIXLrzppmFDkyFWcWxAm+P++urdAjguLACbKB2yPptn62nKByP0g0X2cCODA== X-Received: by 2002:adf:f1c3:: with SMTP id z3mr11079275wro.189.1633082711007; Fri, 01 Oct 2021 03:05:11 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j4sm5408291wrt.67.2021.10.01.03.05.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 03:05:10 -0700 (PDT) Message-Id: <2c8c60c3f310a490217b9b1c62b13fca83c171be.1633082702.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 01 Oct 2021 10:05:02 +0000 Subject: [PATCH 11/11] rebase -m: don't fork git checkout Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Elijah Newren , Phillip Wood , Phillip Wood Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood From: Phillip Wood Now that reset_head() can handle the initial checkout of onto correctly use it in the "merge" backend instead of forking 'git checkout'. This opens the way for us to stop calling the post-checkout hook in the future. Not running 'git checkout' means that 'rebase -i/m' no longer recurse submodules when checking out 'onto' (thanks to Philippe Blain for pointing this out). As the rest of rebase does not know what to do with submodules this is probably a good thing. When using merge-ort rebase ought be able to handle submodules correctly if it parsed the submodule config, such a change is left for a future patch series. The "apply" based rebase has avoided forking git checkout since ac7f467fef ("builtin/rebase: support running "git rebase "", 2018-08-07). The code that handles the checkout was moved into libgit by b309a97108 ("reset: extract reset_head() from rebase", 2020-04-07). Signed-off-by: Phillip Wood --- sequencer.c | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/sequencer.c b/sequencer.c index 36ab6854c6b..9011dd84afd 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4217,42 +4217,26 @@ int apply_autostash_oid(const char *stash_oid) return apply_save_autostash_oid(stash_oid, 1); } -static int run_git_checkout(struct repository *r, struct replay_opts *opts, - const char *commit, const char *action) -{ - struct child_process cmd = CHILD_PROCESS_INIT; - int ret; - - cmd.git_cmd = 1; - - strvec_push(&cmd.args, "checkout"); - strvec_push(&cmd.args, commit); - strvec_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action); - - if (opts->verbose) - ret = run_command(&cmd); - else - ret = run_command_silent_on_success(&cmd); - - if (!ret) - discard_index(r->index); - - return ret; -} - static int checkout_onto(struct repository *r, struct replay_opts *opts, const char *onto_name, const struct object_id *onto, const struct object_id *orig_head) { - const char *action = reflog_message(opts, "start", "checkout %s", onto_name); - - if (run_git_checkout(r, opts, oid_to_hex(onto), action)) { + struct reset_head_opts ropts = { + .oid = onto, + .orig_head = orig_head, + .flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | + RESET_HEAD_RUN_POST_CHECKOUT_HOOK, + .head_msg = reflog_message(opts, "start", "checkout %s", + onto_name), + .default_reflog_action = "rebase" + }; + if (reset_head(r, &ropts)) { apply_autostash(rebase_path_autostash()); sequencer_remove_state(opts); return error(_("could not detach HEAD")); } - return update_ref(NULL, "ORIG_HEAD", orig_head, NULL, 0, UPDATE_REFS_MSG_ON_ERR); + return 0; } static int stopped_at_head(struct repository *r)