* [PATCH 1/2] checkout/restore: refuse unmerging paths unless checking out of the index
@ 2023-07-28 22:06 Junio C Hamano
2023-07-28 22:07 ` [PATCH 2/2] checkout/restore: add basic tests for --merge Junio C Hamano
0 siblings, 1 reply; 2+ messages in thread
From: Junio C Hamano @ 2023-07-28 22:06 UTC (permalink / raw)
To: git
Recreating unmerged index entries using resolve-undo data,
recreating conflicted working tree files using unmerged index
entries, and writing data out of unmerged index entries, make
sense only when we are checking paths out of the index and not when
we are checking paths out of a tree-ish.
Add an extra check to make sure "--merge" and "--ours/--theirs"
options are rejected when checking out from a tree-ish, update the
document (especially the SYNOPSIS section) to highlight that they
are incompatible, and add a few tests to make sure the combination
fails.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
* We do have proper description of the --merge option in
Documentation/git-checkout.txt to cover the operation in both
modes of "checkout" (i.e. checking out a branch vs checking out
paths), but the parse_options help text only talks about the one
that happens when checking out a branch. We might want to do
something about it, but I am not sure what the right phrase
should be. The options[] array there is created by concatenating
common ones, switch-related ones, and restore-related ones, and
ideally how the option works in each mode should be decribed in
the latter two, but we cannot have duplicate entries in the
resulting options[] array, of course, so such an approach would
not work well.
Documentation/git-checkout.txt | 9 ++++++---
Documentation/git-restore.txt | 4 ++++
builtin/checkout.c | 9 +++++++++
t/t2070-restore.sh | 7 +++++--
t/t7201-co.sh | 5 +++++
5 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index 4af0904f47..a30e3ebc51 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -12,8 +12,10 @@ SYNOPSIS
'git checkout' [-q] [-f] [-m] --detach [<branch>]
'git checkout' [-q] [-f] [-m] [--detach] <commit>
'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
-'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...
-'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
+'git checkout' [-f] <tree-ish> [--] <pathspec>...
+'git checkout' [-f] <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]
+'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...
+'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]
'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
DESCRIPTION
@@ -260,7 +262,8 @@ and mark the resolved paths with `git add` (or `git rm` if the merge
should result in deletion of the path).
+
When checking out paths from the index, this option lets you recreate
-the conflicted merge in the specified paths.
+the conflicted merge in the specified paths. This option cannot be
+used when checking out paths from a tree-ish.
+
When switching branches with `--merge`, staged changes may be lost.
diff --git a/Documentation/git-restore.txt b/Documentation/git-restore.txt
index 5964810caa..c70444705b 100644
--- a/Documentation/git-restore.txt
+++ b/Documentation/git-restore.txt
@@ -78,6 +78,8 @@ all modified paths.
--theirs::
When restoring files in the working tree from the index, use
stage #2 ('ours') or #3 ('theirs') for unmerged paths.
+ This option cannot be used when checking out paths from a
+ tree-ish (i.e. with the `--source` option).
+
Note that during `git rebase` and `git pull --rebase`, 'ours' and
'theirs' may appear swapped. See the explanation of the same options
@@ -87,6 +89,8 @@ in linkgit:git-checkout[1] for details.
--merge::
When restoring files on the working tree from the index,
recreate the conflicted merge in the unmerged paths.
+ This option cannot be used when checking out paths from a
+ tree-ish (i.e. with the `--source` option).
--conflict=<style>::
The same as `--merge` option above, but changes the way the
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 715eeb5048..b8dfba57c6 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -520,6 +520,15 @@ static int checkout_paths(const struct checkout_opts *opts,
"--merge", "--conflict", "--staged");
}
+ /*
+ * recreating unmerged index entries and writing out data from
+ * unmerged index entries would make no sense when checking out
+ * of a tree-ish.
+ */
+ if ((opts->merge || opts->writeout_stage) && opts->source_tree)
+ die(_("'%s', '%s', or '%s' cannot be used when checking out of a tree"),
+ "--merge", "--ours", "--theirs");
+
if (opts->patch_mode) {
enum add_p_mode patch_mode;
const char *rev = new_branch_info->name;
diff --git a/t/t2070-restore.sh b/t/t2070-restore.sh
index c5d19dd973..fd775807e7 100755
--- a/t/t2070-restore.sh
+++ b/t/t2070-restore.sh
@@ -137,11 +137,14 @@ test_expect_success 'restore --staged invalidates cache tree for deletions' '
test_must_fail git rev-parse HEAD:new1
'
-test_expect_success 'restore with merge options rejects --staged' '
+test_expect_success 'restore with merge options are incompatible with certain options' '
for opts in \
"--staged --ours" \
"--staged --theirs" \
"--staged --merge" \
+ "--source=HEAD --ours" \
+ "--source=HEAD --theirs" \
+ "--source=HEAD --merge" \
"--staged --conflict=diff3" \
"--staged --worktree --ours" \
"--staged --worktree --theirs" \
@@ -149,7 +152,7 @@ test_expect_success 'restore with merge options rejects --staged' '
"--staged --worktree --conflict=zdiff3"
do
test_must_fail git restore $opts . 2>err &&
- grep "cannot be used with --staged" err || return
+ grep "cannot be used" err || return
done
'
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 61ad47b0c1..23d4dadbcc 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -497,6 +497,11 @@ test_expect_success 'checkout unmerged stage' '
test ztheirside = "z$(cat file)"
'
+test_expect_success 'checkout path with --merge from tree-ish is a no-no' '
+ setup_conflicting_index &&
+ test_must_fail git checkout -m HEAD -- file
+'
+
test_expect_success 'checkout with --merge' '
setup_conflicting_index &&
echo "none of the above" >sample &&
--
2.41.0-478-gee48e70a82
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/2] checkout/restore: add basic tests for --merge
2023-07-28 22:06 [PATCH 1/2] checkout/restore: refuse unmerging paths unless checking out of the index Junio C Hamano
@ 2023-07-28 22:07 ` Junio C Hamano
0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2023-07-28 22:07 UTC (permalink / raw)
To: git
Even though "checkout --merge -- paths" had some tests, we never
made sure it worked to recreate the conflicted state _after_ the
resolution was recorded in the index. Also "restore --merge" did
not even have any tests.
Currently these commands use the unmerge_marked_index() interface
that cannot handle paths that have been resolved as removal, and
tests for that case are marked with test_expect_failure; these
should eventually be fixed, but not in this patch.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
t/t2070-restore.sh | 64 ++++++++++++++++++++++++++++++++++++++++++++++
t/t7201-co.sh | 42 ++++++++++++++++++++++++++++++
2 files changed, 106 insertions(+)
diff --git a/t/t2070-restore.sh b/t/t2070-restore.sh
index fd775807e7..d97ecc2483 100755
--- a/t/t2070-restore.sh
+++ b/t/t2070-restore.sh
@@ -137,6 +137,70 @@ test_expect_success 'restore --staged invalidates cache tree for deletions' '
test_must_fail git rev-parse HEAD:new1
'
+test_expect_success 'restore --merge to unresolve' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ echo nothing >file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
+test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ echo nothing >file &&
+ git add file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
+test_expect_failure 'restore --merge to unresolve after (mistaken) resolution' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ git rm -f file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
test_expect_success 'restore with merge options are incompatible with certain options' '
for opts in \
"--staged --ours" \
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 23d4dadbcc..4b07a26c14 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -522,6 +522,48 @@ test_expect_success 'checkout with --merge' '
test_cmp merged file
'
+test_expect_success 'checkout -m works after (mistaken) resolution' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ # resolve to something
+ git add file &&
+ git checkout --merge -- fild file filf &&
+ {
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ } >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_failure 'checkout -m works after (mistaken) resolution to remove' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ # resolve to remove
+ git rm file &&
+ git checkout --merge -- fild file filf &&
+ {
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ } >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
test_expect_success 'checkout with --merge, in diff3 -m style' '
git config merge.conflictstyle diff3 &&
setup_conflicting_index &&
--
2.41.0-478-gee48e70a82
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-07-28 22:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-28 22:06 [PATCH 1/2] checkout/restore: refuse unmerging paths unless checking out of the index Junio C Hamano
2023-07-28 22:07 ` [PATCH 2/2] checkout/restore: add basic tests for --merge Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).