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

Re: [PATCH 1/2] diffcore-break: don't divide by zero

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 2, 2013, 21:15 UTC
Message-ID
<7vvc84ab2y.fsf@alter.siamese.dyndns.org>
In-Reply-To
<a2b6c61371ac6ff1e180c6600e57499ff94b2fd2.1364931627.git.john@keeping.me.uk>
John Keeping <john@keeping.me.uk> writes:
Show 5 quoted lines
> When the source file is empty, the calculation of the merge score
> results in a division by zero.  Since the merge score is initialized to
> zero, it makes sense to just leave it as it is if the source size is
> zero.  This means that we still use the extent of damage metric to
> decide whether to break the filepair.

Well spotted. An empty blob to another blob that is larger than 400 bytes will trigger div0, and it makes sense to leave merge-score to 0 (i.e. do not show as whole-delete-then-whole-add after rename detection is done and the broken filepair is merged back).

Actually, if src->size is 0, we probably shouldn't break the filepair in the first place. That is, if your preimage and postimage looked like these:

     == preimage ==		== postimage ==
     F (empty file)		F (a large file)
			        E (a new empty file)

do we want to see F renamed to E and then a new file created as F while running "git diff -B -M"? I doubt it.

So in that sense, this might be a better solution.  I dunno.
 diffcore-break.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/diffcore-break.c b/diffcore-break.c
index 44f8678..eabafd5 100644
--- a/diffcore-break.c
+++ b/diffcore-break.c
@@ -67,6 +67,8 @@ static int should_break(struct diff_filespec *src,
 	max_size = ((src->size > dst->size) ? src->size : dst->size);
 	if (max_size < MINIMUM_BREAK_SIZE)
 		return 0; /* we do not break too small filepair */
+	if (src->size == 0)
+		return 0; /* we do not let empty files get renamed */
 
 	if (diffcore_count_changes(src, dst,
 				   &src->cnt_data, &dst->cnt_data,
Previous: John KeepingNext: John Keeping
Message 3 of 9 in “Fixes for undefined behaviour”
  1. 0/2 Fixes for undefined behaviourJohn Keeping, Apr 2, 2013
  2. 1/2 diffcore-break: don't divide by zeroJohn Keeping, Apr 2, 2013
  3. Junio C HamanoApr 2, 2013
  4. John KeepingApr 2, 2013
  5. Junio C HamanoApr 2, 2013
  6. diffcore-break: don't divide by zeroJohn Keeping, Apr 3, 2013
  7. 2/2 bisect: avoid signed integer overflowJohn Keeping, Apr 2, 2013
  8. Junio C HamanoApr 2, 2013
  9. John KeepingApr 3, 2013

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.