git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [bug?] checkout -m doesn't work without a base version

From
PHPete Harlan <pgit@pcharlan.com>
Date
Dec 7, 2011, 07:30 UTC
Message-ID
<4EDF1631.5090906@pcharlan.com>
In-Reply-To
<7vbormn8vk.fsf@alter.siamese.dyndns.org>
On 12/05/2011 10:58 AM, Junio C Hamano wrote:
Show 18 quoted lines
> Pete Harlan <pgit@pcharlan.com> writes:
> 
>> But this only works if there's a base version; if foo.c was added in
>> each branch, we get:
>>
>>    error: path 'foo.c' does not have all three versions
>>
>> Git didn't need all three versions to create the original conflicted
>> file, so why would it need them to recreate it?
> 
> Because the original "merge" was a bit more carefully written but
> "checkout -m" was written without worrying too much about "both sides
> added differently" corner case and still being defensive about not doing
> random thing upon getting an unexpected input state.
> 
> IOW, being lazy ;-)
> 
> How does this look?

Wow, thanks for the quick response! That does indeed let me checkout the file as expected.

I wrote a test (below) to be folded in with your patch, but the test fails because it expects the restored file to be the same as the originally-conflicted file, but the conflict-line labels change from "HEAD" and "master":

  <<<<<<< HEAD
  in_topic
  =======
  in_master
  >>>>>>> master

to "ours" and "theirs". (The same thing happens in the 3-way merge case.)

If the label change is expected then I can rewrite the test to ignore labels, or to expect "ours" and "theirs", whichever you think is best. If the label change is unexpected, then I guess the test is good :)

--Pete

-- >8 -- Test 'checkout -m -- path' when path is a 2-way merge

Signed-off-by: Pete Harlan <pgit@pcharlan.com>
---
 t/t2023-checkout-m-twoway.sh |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)
 create mode 100755 t/t2023-checkout-m-twoway.sh
diff --git a/t/t2023-checkout-m-twoway.sh b/t/t2023-checkout-m-twoway.sh
new file mode 100755
index 0000000..5b50360
--- /dev/null
+++ b/t/t2023-checkout-m-twoway.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='checkout -m -- conflicted/path/with/2-way/merge
+
+Ensures that checkout -m on a resolved file restores the conflicted file
+when it conflicted with a 2-way merge.'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_tick &&
+	test_commit initial_commit &&
+	git branch topic &&
+	test_commit added_in_master file.txt in_master &&
+	git checkout topic &&
+	test_commit added_in_topic file.txt in_topic
+'
+
+test_must_fail git merge master
+
+test_expect_success '-m restores 2-way conflicted+resolved file' '
+	cp file.txt file.txt.conflicted &&
+	echo resolved >file.txt &&
+	git add file.txt &&
+	git checkout -m -- file.txt &&
+	test_cmp file.txt.conflicted file.txt
+'
+
+test_done
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 15 in “[bug?] checkout -m doesn't work without a base version”
  1. Pete HarlanDec 4, 2011
  2. Junio C HamanoDec 5, 2011
  3. Pete HarlanDec 7, 2011
  4. Junio C HamanoDec 8, 2011
  5. Pete HarlanDec 12, 2011
  6. Junio C HamanoDec 12, 2011
  7. t/t2023-checkout-m.sh: fix use of test_must_failÆvar Arnfjörð Bjarmason, Dec 20, 2011
  8. Junio C HamanoDec 20, 2011
  9. Michael SchubertDec 14, 2011
  10. Junio C HamanoDec 14, 2011
  11. Miles BaderDec 15, 2011
  12. Michael SchubertDec 15, 2011
  13. Andreas SchwabDec 15, 2011
  14. Junio C HamanoDec 15, 2011
  15. Ramsay JonesDec 16, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.