From patchwork Thu Jul 27 21:46:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13330797 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA94DEB64DD for ; Thu, 27 Jul 2023 21:46:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231347AbjG0Vq3 (ORCPT ); Thu, 27 Jul 2023 17:46:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbjG0Vq2 (ORCPT ); Thu, 27 Jul 2023 17:46:28 -0400 Received: from pb-smtp20.pobox.com (pb-smtp20.pobox.com [173.228.157.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A76B52135 for ; Thu, 27 Jul 2023 14:46:27 -0700 (PDT) Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 204AE22DEC; Thu, 27 Jul 2023 17:46:27 -0400 (EDT) (envelope-from junio@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:mime-version:content-type; s=sasl; bh=D 9KACuvo9SrMqm1diBjgSZ+nQvcirkqZLiLPSbLCBiY=; b=kQ0M87EIxDE21yP+Z 236F+3bLck11Pmz5/qPyhtMf1AsiSkPBz+mff5ug6TsIrXXBd9Z/O8VNRZiIWeCd CT7I1xH9IAvcfbKXNd73Gyanv4wRUwP/EnpxMay/x+Aa9g5ZJ8vwXp8dnxSW2FCt iphV1dhKAOpdHrCEoOAeENWEmw= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 0B62322DEB; Thu, 27 Jul 2023 17:46:27 -0400 (EDT) (envelope-from junio@pobox.com) Received: from pobox.com (unknown [34.168.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp20.pobox.com (Postfix) with ESMTPSA id 7E13022DE9; Thu, 27 Jul 2023 17:46:23 -0400 (EDT) (envelope-from junio@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 1/2] resolve-undo: allow resurrecting conflicted state that resolved to deletion Date: Thu, 27 Jul 2023 14:46:22 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Pobox-Relay-ID: 07C1CAC4-2CC7-11EE-A3A4-C2DA088D43B2-77302942!pb-smtp20.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The resolve-undo index extension records up to three (mode, object name) tuples for non-zero stages for each path that was resolved, to be used to recreate the original conflicted state later when the user requests. The unmerge_index_entry_at() function uses the resolve-undo data to do so, but it assumes that the path for which the conflicted state needs to be recreated can be specified by the position in the active_cache[] array. This obviously cannot salvage a conflicted state that was resolved by removing them. A delete-modify conflict, in which the change whose "modify" side made is a trivial typofix, may legitimately be resolved to remove the path, and resolve-undo extension does record the two (mode, object name) tuples for the common ancestor version and their version, lacking our version. Introduce a new unmerge_index_entry() helper function that takes the path (which does not necessarily have to exist in the active_cache[] array) and resolve-undo data, and use it to reimplement unmerge_index() public function that is used by "git rerere". The limited interface is still kept for now, as it is used by "git checkout -m" and "git update-index --unmerge", but these two codepaths will be updated to lift the assumption to allow conflicts that resolved to deletion can be recreated. Signed-off-by: Junio C Hamano --- * No new tests, as unmerge_index() that is used by "git rerere" already exercises the codepath. We'll use the new function also in "update-index" in a future patch. resolve-undo.c | 46 +++++++++++++++++++++++++++++++++++++++++----- resolve-undo.h | 1 + 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/resolve-undo.c b/resolve-undo.c index 7817f5d6db..9ceab129ac 100644 --- a/resolve-undo.c +++ b/resolve-undo.c @@ -184,19 +184,55 @@ void unmerge_marked_index(struct index_state *istate) } } +int unmerge_index_entry(struct index_state *istate, const char *path, + struct resolve_undo_info *ru) +{ + int i = index_name_pos(istate, path, strlen(path)); + + if (i < 0) { + /* unmerged? */ + i = -i - 1; + if (i < istate->cache_nr && + !strcmp(istate->cache[i]->name, path)) + /* yes, it is already unmerged */ + return 0; + /* fallthru: resolved to removal */ + } else { + /* merged - remove it to replace it with unmerged entries */ + remove_index_entry_at(istate, i); + } + + for (i = 0; i < 3; i++) { + struct cache_entry *ce; + if (!ru->mode[i]) + continue; + ce = make_cache_entry(istate, ru->mode[i], &ru->oid[i], + path, i + 1, 0); + if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD)) + return error("cannot unmerge '%s'", path); + } + return 0; +} + void unmerge_index(struct index_state *istate, const struct pathspec *pathspec) { - int i; + struct string_list_item *item; if (!istate->resolve_undo) return; /* TODO: audit for interaction with sparse-index. */ ensure_full_index(istate); - for (i = 0; i < istate->cache_nr; i++) { - const struct cache_entry *ce = istate->cache[i]; - if (!ce_path_match(istate, ce, pathspec, NULL)) + + for_each_string_list_item(item, istate->resolve_undo) { + const char *path = item->string; + struct resolve_undo_info *ru = item->util; + if (!item->util) + continue; + if (!match_pathspec(istate, pathspec, + item->string, strlen(item->string), + 0, NULL, 0)) continue; - i = unmerge_index_entry_at(istate, i); + unmerge_index_entry(istate, path, ru); } } diff --git a/resolve-undo.h b/resolve-undo.h index c5deafc92f..1ae321c88b 100644 --- a/resolve-undo.h +++ b/resolve-undo.h @@ -18,6 +18,7 @@ void resolve_undo_write(struct strbuf *, struct string_list *); struct string_list *resolve_undo_read(const char *, unsigned long); void resolve_undo_clear_index(struct index_state *); int unmerge_index_entry_at(struct index_state *, int); +int unmerge_index_entry(struct index_state *, const char *, struct resolve_undo_info *); void unmerge_index(struct index_state *, const struct pathspec *); void unmerge_marked_index(struct index_state *); From patchwork Thu Jul 27 21:51:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13330798 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3166BEB64DD for ; Thu, 27 Jul 2023 21:51:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230043AbjG0VvP (ORCPT ); Thu, 27 Jul 2023 17:51:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjG0VvO (ORCPT ); Thu, 27 Jul 2023 17:51:14 -0400 Received: from pb-smtp20.pobox.com (pb-smtp20.pobox.com [173.228.157.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96C372135 for ; Thu, 27 Jul 2023 14:51:13 -0700 (PDT) Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 4B8EB22E7F; Thu, 27 Jul 2023 17:51:13 -0400 (EDT) (envelope-from junio@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=5Glvn+ARik6YlsAnmdCZqw9AgUbBLkv7RvDjN8 JY27Q=; b=GD0xwDpeHu4Zndbf8Vx53dIxD1XL9c4oc3zdoEbGDmIB+vQ4HQhkoy tbJExDY+d+5vMu7wsz5HP8+/GM46PgAUsDJ5kWaUikB/fxSWja/48Nm9YUuWRc4v XoV0aT65J4PwACf9QKbBL5oLBbmMVkdj3dgKTRQj4aK8JbGuuTpI0= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 43DF222E7E; Thu, 27 Jul 2023 17:51:13 -0400 (EDT) (envelope-from junio@pobox.com) Received: from pobox.com (unknown [34.168.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp20.pobox.com (Postfix) with ESMTPSA id C700122E7C; Thu, 27 Jul 2023 17:51:09 -0400 (EDT) (envelope-from junio@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 2/2] update-index: use unmerge_index_entry() to support removal References: Date: Thu, 27 Jul 2023 14:51:08 -0700 In-Reply-To: (Junio C. Hamano's message of "Thu, 27 Jul 2023 14:46:22 -0700") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Pobox-Relay-ID: B2677852-2CC7-11EE-9190-C2DA088D43B2-77302942!pb-smtp20.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org "update-index --unresolve" used the unmerge_index_entry_at() that assumed that the path to be unresolved must be in the index, which made it impossible to unresolve a path that was resolved as removal. Rewrite unresolve_one() to use the unmerge_index_entry() to support unresolving such a path. Signed-off-by: Junio C Hamano --- * Now getting rid of unmerge_index_entry_at() is a bigger task. The only remaining user of it is the "checkout -m -- $path" codepath. builtin/update-index.c | 33 +++++++++++++++++---------------- t/t2030-unresolve-info.sh | 29 +++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/builtin/update-index.c b/builtin/update-index.c index 853ec9eb7a..d02ac55313 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -661,26 +661,26 @@ static int unresolve_one(const char *path) int pos; int ret = 0; struct cache_entry *ce_2 = NULL, *ce_3 = NULL; + struct resolve_undo_info *ru = NULL; + + if (the_index.resolve_undo) { + struct string_list_item *item; + item = string_list_lookup(the_index.resolve_undo, path); + if (item) + ru = item->util; + } + + /* resolve-undo record exists for the path */ + if (ru) + return unmerge_index_entry(&the_index, path, ru); /* See if there is such entry in the index. */ pos = index_name_pos(&the_index, path, namelen); if (0 <= pos) { - /* already merged */ - pos = unmerge_index_entry_at(&the_index, pos); - if (pos < the_index.cache_nr) { - const struct cache_entry *ce = the_index.cache[pos]; - if (ce_stage(ce) && - ce_namelen(ce) == namelen && - !memcmp(ce->name, path, namelen)) - return 0; - } - /* no resolve-undo information; fall back */ + ; /* resolve-undo record was used already -- fall back */ } else { - /* If there isn't, either it is unmerged, or - * resolved as "removed" by mistake. We do not - * want to do anything in the former case. - */ - pos = -pos-1; + /* Is it unmerged? */ + pos = -pos - 1; if (pos < the_index.cache_nr) { const struct cache_entry *ce = the_index.cache[pos]; if (ce_namelen(ce) == namelen && @@ -688,9 +688,10 @@ static int unresolve_one(const char *path) fprintf(stderr, "%s: skipping still unmerged path.\n", path); - goto free_return; } + goto free_return; } + /* No, such a path does not exist -- removed */ } /* diff --git a/t/t2030-unresolve-info.sh b/t/t2030-unresolve-info.sh index d4e7760df5..6a3af64e0f 100755 --- a/t/t2030-unresolve-info.sh +++ b/t/t2030-unresolve-info.sh @@ -37,11 +37,17 @@ prime_resolve_undo () { git checkout second^0 && test_tick && test_must_fail git merge third^0 && - echo merge does not leave anything && check_resolve_undo empty && - echo different >fi/le && - git add fi/le && - echo resolving records && + + # how should the conflict be resolved? + case "$1" in + remove) + rm -f file/le && git rm fi/le + ;; + *) # modify + echo different >fi/le && git add fi/le + ;; + esac check_resolve_undo recorded fi/le initial:fi/le second:fi/le third:fi/le } @@ -134,6 +140,21 @@ test_expect_success 'unmerge can be done even after committing' ' test_line_count = 3 actual ' +test_expect_success 'unmerge removal' ' + prime_resolve_undo remove && + git update-index --unresolve fi/le && + git ls-files -u actual && + test_line_count = 3 actual +' + +test_expect_success 'unmerge removal after committing' ' + prime_resolve_undo remove && + git commit -m "record to nuke MERGE_HEAD" && + git update-index --unresolve fi/le && + git ls-files -u actual && + test_line_count = 3 actual +' + test_expect_success 'rerere and rerere forget' ' mkdir .git/rr-cache && prime_resolve_undo && From patchwork Fri Jul 28 19:49:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13332614 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2529AC001DE for ; Fri, 28 Jul 2023 19:49:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233897AbjG1Tt1 (ORCPT ); Fri, 28 Jul 2023 15:49:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229680AbjG1Tt0 (ORCPT ); Fri, 28 Jul 2023 15:49:26 -0400 Received: from pb-smtp2.pobox.com (pb-smtp2.pobox.com [64.147.108.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F94D3C07 for ; Fri, 28 Jul 2023 12:49:25 -0700 (PDT) Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 620421A87A6; Fri, 28 Jul 2023 15:49:24 -0400 (EDT) (envelope-from junio@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=DsEy1vsUG9H+2/ys9XbVeHINDxmmQuFe+AqEPe /KW84=; b=g0M81vcpCwSOS7/4WUC4cloM7QHh1eYqwnPtXsyV+6Y7yGjMk4HMtv B8oPygXYrRwGok3JvNcj6Hg6uJjCLDcZelnFWxYC4r3/8RW5gPJXLvzUHb+z3G0D 4Sa+5LA3Vo6ZmdLeiCgLj5nNLQGJ4z2rvLcIhJrnZIisxqjIkZHbo= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 59A381A87A5; Fri, 28 Jul 2023 15:49:24 -0400 (EDT) (envelope-from junio@pobox.com) Received: from pobox.com (unknown [34.168.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id A93001A87A4; Fri, 28 Jul 2023 15:49:23 -0400 (EDT) (envelope-from junio@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 3/2] update-index: remove stale fallback code for "--unresolve" References: Date: Fri, 28 Jul 2023 12:49:22 -0700 In-Reply-To: (Junio C. Hamano's message of "Thu, 27 Jul 2023 14:51:08 -0700") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Pobox-Relay-ID: DA077A34-2D7F-11EE-8F2C-307A8E0A682E-77302942!pb-smtp2.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The "update-index --unresolve" is a relatively old feature that was introduced in Git v1.4.1 (June 2006), which predates the resolve-undo extension introduced in Git v1.7.0 (February 2010). The original code that was limited only to work during a merge (and not during a rebase or a cherry-pick) has been kept as the fallback codepath to be used as a transition measure. By now, for more than 10 years we have stored resolve-undo extension in the index file, and the fallback code way outlived its usefulness. Remove it, together with two file-scope static global variables. One of these variables is still used by surviving function, but it does not have to be a global at all, so move it to local to that function. Signed-off-by: Junio C Hamano --- builtin/update-index.c | 103 ++++------------------------------------- 1 file changed, 8 insertions(+), 95 deletions(-) diff --git a/builtin/update-index.c b/builtin/update-index.c index d02ac55313..79c25a2a58 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -609,9 +609,6 @@ static const char * const update_index_usage[] = { NULL }; -static struct object_id head_oid; -static struct object_id merge_head_oid; - static struct cache_entry *read_one_ent(const char *which, struct object_id *ent, const char *path, int namelen, int stage) @@ -640,102 +637,17 @@ static struct cache_entry *read_one_ent(const char *which, return ce; } -static int read_head_pointers(void) -{ - static int result = -2; /* unknown yet */ - - if (result == -2) { - result = -1; - if (read_ref("HEAD", &head_oid)) - return error("No HEAD -- no initial comit yet?"); - if (read_ref("MERGE_HEAD", &merge_head_oid)) - return error("Not in the middle of a merge"); - result = 0; - } - return result; -} - static int unresolve_one(const char *path) { - int namelen = strlen(path); - int pos; - int ret = 0; - struct cache_entry *ce_2 = NULL, *ce_3 = NULL; - struct resolve_undo_info *ru = NULL; - - if (the_index.resolve_undo) { - struct string_list_item *item; - item = string_list_lookup(the_index.resolve_undo, path); - if (item) - ru = item->util; - } - - /* resolve-undo record exists for the path */ - if (ru) - return unmerge_index_entry(&the_index, path, ru); + struct string_list_item *item; - /* See if there is such entry in the index. */ - pos = index_name_pos(&the_index, path, namelen); - if (0 <= pos) { - ; /* resolve-undo record was used already -- fall back */ - } else { - /* Is it unmerged? */ - pos = -pos - 1; - if (pos < the_index.cache_nr) { - const struct cache_entry *ce = the_index.cache[pos]; - if (ce_namelen(ce) == namelen && - !memcmp(ce->name, path, namelen)) { - fprintf(stderr, - "%s: skipping still unmerged path.\n", - path); - } - goto free_return; - } - /* No, such a path does not exist -- removed */ - } - - /* - * We are not using resolve-undo information but just - * populating the stages #2 and #3 from HEAD and MERGE_HEAD. - * - * This is a flawed replacement of true "unresolve", as we do - * not have a way to recreate the stage #1 for the common - * ancestor (which may not be a unique merge-base between the - * two). - */ - if (read_head_pointers()) { - ret = -1; - goto free_return; - } - - ce_2 = read_one_ent("our", &head_oid, path, namelen, 2); - ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3); - - if (!ce_2 || !ce_3) { - ret = -1; - goto free_return; - } - if (oideq(&ce_2->oid, &ce_3->oid) && - ce_2->ce_mode == ce_3->ce_mode) { - fprintf(stderr, "%s: identical in both, skipping.\n", - path); - goto free_return; - } - - remove_file_from_index(&the_index, path); - if (add_index_entry(&the_index, ce_2, ADD_CACHE_OK_TO_ADD)) { - error("%s: cannot add our version to the index.", path); - ret = -1; - goto free_return; - } - if (!add_index_entry(&the_index, ce_3, ADD_CACHE_OK_TO_ADD)) + if (!the_index.resolve_undo) return 0; - error("%s: cannot add their version to the index.", path); - ret = -1; - free_return: - discard_cache_entry(ce_2); - discard_cache_entry(ce_3); - return ret; + item = string_list_lookup(the_index.resolve_undo, path); + if (!item) + return 0; /* no resolve-undo record for the path */ + + return unmerge_index_entry(&the_index, path, item->util); } static int do_unresolve(int ac, const char **av, @@ -762,6 +674,7 @@ static int do_reupdate(const char **paths, int pos; int has_head = 1; struct pathspec pathspec; + struct object_id head_oid; parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,