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

Re: [PATCH 4/6] apply: make sure check_preimage() does not leave empty file on error

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 28, 2015, 18:35 UTC
Message-ID
<xmqqbn9al8v9.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1451181092-26054-5-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 23 quoted lines
> diff --git a/t/t2203-add-intent.sh b/t/t2203-add-intent.sh
> index bb5ef2b..96c8755 100755
> --- a/t/t2203-add-intent.sh
> +++ b/t/t2203-add-intent.sh
> @@ -95,5 +95,21 @@ test_expect_success 'apply adds new file on i-t-a entry' '
>  	)
>  '
>  
> +test_expect_success 'apply:check_preimage() not creating empty file' '
> +	git init check-preimage &&
> +	(
> +		cd check-preimage &&
> +		echo oldcontent >newfile &&
> +		git add newfile &&
> +		echo newcontent >newfile &&
> +		git diff >patch &&
> +		rm .git/index &&
> +		git add -N newfile &&
> +		rm newfile &&
> +		test_must_fail git apply -3 patch &&
> +		! test -f newfile
> +	)
> +'

Unlike "apply --index" (and "apply" without any option to work on working tree), "apply -3" is allowed to write to the working tree even when it fails, if the failure comes from a merge conflict and that is by design. In this case, the patch expects that there is an existing file with 'oldcontent' so it should try to do a three-way merge to go from an 'unspecified' content (i.e. the current state in the index) in a way similar to going from 'oldcontent' to 'newcontent', and because we _have_ to make sure that the path that holds the 'unspecified' content is clean between the index and the working tree--but an earlier "add -N" said that "the index knows about the path, I am not telling what its contents are yet", by definition the index and the working tree cannot match so the application _must_ fail, before even attempting to do a three-way merge, hence it should not touch the working tree.

So expecting a failure from "apply -3" sounds like the right thing to do, and obviously it should not leave any "newfile". The test also must check that the index has not been modified since the last "git add -N" (most notably, we want to see 'newfile' still has the I-T-A bit on).

More interesting cases to test would be:
 - Instead of "rm newfile", have ">newfile" to empty the contents
   before applying the patch with "apply -3".  We should expect the
   same "apply -3 fails", but we should see an empty 'newfile' in
   the working tree.  The index must be the same as the last "git
   add -N".
 - Instead of "rm newfile", have "echo oldcontent >newfile".  What
   should "apply -3" do?  The patch would be applicable with "apply"
   without any argument (hence without involving the index), but
   "-3" requires 'check_index', and again any entry with I-T-A bit
   on by definition does not match between the index and the working
   tree, so the application must fail, I think.
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 11 of 15 in “nd/ita-cleanup updates”
  1. 0/6 nd/ita-cleanup updatesNguyễn Thái Ngọc Duy, Dec 27, 2015
  2. 1/6 blame: remove obsolete commentNguyễn Thái Ngọc Duy, Dec 27, 2015
  3. Junio C HamanoDec 28, 2015
  4. 2/6 Add and use convenient macro ce_intent_to_add()Nguyễn Thái Ngọc Duy, Dec 27, 2015
  5. Junio C HamanoDec 28, 2015
  6. 3/6 apply: fix adding new files on i-t-a entriesNguyễn Thái Ngọc Duy, Dec 27, 2015
  7. Junio C HamanoDec 28, 2015
  8. Duy NguyenDec 29, 2015
  9. Junio C HamanoDec 29, 2015
  10. 4/6 apply: make sure check_preimage() does not leave empty file on errorNguyễn Thái Ngọc Duy, Dec 27, 2015
  11. Junio C HamanoDec 28, 2015
  12. 5/6 checkout(-index): do not checkout i-t-a entriesNguyễn Thái Ngọc Duy, Dec 27, 2015
  13. Junio C HamanoDec 28, 2015
  14. 6/6 grep: make it clear i-t-a entries are ignoredNguyễn Thái Ngọc Duy, Dec 27, 2015
  15. Junio C HamanoDec 28, 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.