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

Re: [RFC] git-am: handling unborn branches

From
Paul Tan <pyokagan@gmail.com>
Date
Jun 5, 2015, 16:26 UTC
Message-ID
<CACRoPnTjqWrhS0UArSevjp6kUbQL8YYEvvobmykssoo05wjbow@mail.gmail.com>
In-Reply-To
<xmqqfv66tbei.fsf@gitster.dls.corp.google.com>
On Fri, Jun 5, 2015 at 11:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> Paul Tan <pyokagan@gmail.com> writes:
>
>> Hmm, actually git-am.sh doesn't seem to do that even when we have a
>> history to apply patches to. This is okay in the non-3way case, as
>> git-apply will check to see if the patch applies before it modifies
>> the index, but if we fall back on 3-way merge, any new files the
>> failed merge added will not be deleted in the "git read-tree --reset
>> -u HEAD HEAD".
>>
>> Should we do that?
>
> That sounds like the right thing to do; I agree that fixing it may
> or may not be outside the scope of the immediate series.

Hmm, thinking about it, the equivalent C code would be greatly affected by whatever behavior we go with, so I think we should try fixing the behavior first.

This was done really quickly, but I think this may fix it:
diff --git a/git-am.sh b/git-am.sh
index 761befb..f7d54bf 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -502,7 +502,9 @@ then
         ;;
     t,)
         git rerere clear
-        git read-tree --reset -u HEAD HEAD
+        git read-tree --reset HEAD HEAD &&
+        our_tree=$(git write-tree) &&
+        git read-tree -m -u $our_tree HEAD
         orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
         git reset HEAD
         git update-ref ORIG_HEAD $orig_head
diff --git a/t/t4153-am-clean-index.sh b/t/t4153-am-clean-index.sh
new file mode 100755
index 0000000..6d696db
--- /dev/null
+++ b/t/t4153-am-clean-index.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='test clean index with am'
+. ./test-lib.sh
+
+test_expect_success setup '
+    test_commit initial file &&
+    test_commit master-commit file &&
+    git checkout -b conflict master^ &&
+    echo conflict-commit >file &&
+    echo newfile >newfile &&
+    git add newfile &&
+    test_tick &&
+    git commit -a -m conflict-commit &&
+    git format-patch --stdout initial >conflict.patch &&
+    git checkout master
+'
+
+test_expect_success 'am -3 pauses on conflict' '
+    test_must_fail git am -3 conflict.patch &&
+    echo newfile >expected &&
+    test_cmp newfile expected
+'
+
+test_expect_success 'am --skip removes newfile' '
+    git am --skip &&
+    test_path_is_missing newfile
+'
+
+test_done

However, it assumes that the contents of the index are from the failed
merge. If the user modified the index before running git-am --skip,
e.g. the user added a file, then that file would be deleted, which may
not be desired...

Thanks,
Paul
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 8 in “[RFC] git-am: handling unborn branches”
  1. Paul TanJun 4, 2015
  2. Junio C HamanoJun 4, 2015
  3. Paul TanJun 5, 2015
  4. Junio C HamanoJun 5, 2015
  5. Paul TanJun 5, 2015
  6. Junio C HamanoJun 5, 2015
  7. Stefan BellerJun 4, 2015
  8. Paul TanJun 5, 2015

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.