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

[Bug?] Information around newlines lost in merge

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jun 20, 2023, 09:22 UTC
Message-ID
<CAOLa=ZRumrpfG8FxQQG=Q8tGvxEapMvOx6SZyRPh0GSpn5iuUg@mail.gmail.com>

When merging two files which contain newlines at the end, the blob created (with conflicts) is the same as two files created without newlines at the end.

We can demonstrate the same using this simple script which uses `git-merge-tree(1)`. The script creates two files in master, newBranch and then merges the tree. The created files seem to have the same content, eventhough the source files differed by newlines.

#!/usr/bin/env bash

git init git commit --allow-empty -m "base commit"

git branch newBranch

echo -ne "\nA\n" > newline echo -ne "\nA" > half git add . git commit -m "master commit"

git checkout newBranch

echo -ne "\nB\n" > newline echo -ne "\nB" > half git add . git commit -m "branch commit"

treeOID=$(git merge-tree master newBranch | head -n1) halfOID=$(git cat-file -p $treeOID | grep half | awk '{print $3}') newlineOID=$(git cat-file -p $treeOID | grep newline | awk '{print $3}')

if [[ $halfOID == $newlineOID ]]; then
     exit 1
else
     exit 0
fi

Is this expected behavior? Shouldn't the two merged files (newline, half) differ? i.e., shouldn't the file (newline) with newlines at the end contain an extra newline compared to the file without the newline? (half).

If this is expected behavior, what would be the best way to differentiate the two? This is not a bug introduced, but rather the behavior since, the start, which makes me think that I'm missing something (verified via git bisect on latest git).

The context of this is that it is hard to programmatically resolve conflicts without loosing information, since we don't know if there should be a newline suffix in the file or not.

Next: Junio C Hamano
Message 1 of 3 in “[Bug?] Information around newlines lost in merge”
  1. Karthik NayakJun 20, 2023
  2. Junio C HamanoJun 20, 2023
  3. Karthik NayakJun 21, 2023

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.