githome-assistant

How to apply a git diff patch?


Trying to run some tests on core codebase locally, I did the following:

git clone https://github.com/home-assistant/core.git

# create & activate venv ...

# Go to base commit
git checkout <old_commit>

# Extract difference
git diff <old_commit> <new_commit> > diff.patch

# Modify patch & keep only target files by deleting extra content

The issue happens in this step

# Apply the patch
git apply diff.patch
error: No valid patches in input (allow with "--allow-empty")

# After saving the patch with encoding utf-8:
git apply diff.patch
diff.patch:7: trailing whitespace.
"""BLE provisioning helpers for Shelly integration."""
diff.patch:8: trailing whitespace.

diff.patch:9: trailing whitespace.
from __future__ import annotations
diff.patch:10: trailing whitespace.

diff.patch:11: trailing whitespace.
import asyncio
error: patch failed: homeassistant/components/shelly/config_flow.py:2
error: homeassistant/components/shelly/config_flow.py: patch does not apply
error: patch failed: homeassistant/components/shelly/const.py:36
error: homeassistant/components/shelly/const.py: patch does not apply
error: patch failed: homeassistant/components/shelly/manifest.json:1
error: homeassistant/components/shelly/manifest.json: patch does not apply
error: patch failed: homeassistant/components/shelly/strings.json:2
error: homeassistant/components/shelly/strings.json: patch does not apply
error: patch failed: homeassistant/generated/bluetooth.py:702
error: homeassistant/generated/bluetooth.py: patch does not apply
error: patch failed: tests/components/shelly/test_config_flow.py:1
error: tests/components/shelly/test_config_flow.py: patch does not apply

Why does it fail to apply changes though the path itself is derived from direct difference between the 2 commits?

BTW both commits are directly linked & on the same branch dev.

Example to reproduce using this PR:

As shown in the PR merged commit the parent commit hash is 2f7d74f & the merge commit is 7099064.

# Clean working space
git checkout dev

# Go to old commit & git diff between it & next commit
git checkout 2f7d74f
git diff 2f7d74f 7099064 > diff.patch    # or golden.patch any name

# Modify the patch file
# I don't introduce any corruption I just delete whole file changes & keep some.
# Even if I don't modify it at all the error above still appears

# Apply changes (important while you're on old_commit)
git apply diff.patch
error: patch failed: homeassistant/components/sfr_box/config_flow.py:39
error: homeassistant/components/sfr_box/config_flow.py: patch does not apply
error: patch failed: homeassistant/components/sfr_box/quality_scale.yaml:1
error: homeassistant/components/sfr_box/quality_scale.yaml: patch does not apply

Now this should apply the changes to current files but the mentioned error shows up.


Solution

  • I found the issue came from file line endings!

    Normally when you do:

    git diff old new > diff.patch
    

    The resulting file (for me on Windows 10) has this encoding & line ending:

    original encoding & line ending


    Change encoding to utf-8 & change line ending from CRLF to LF:

    Save to utf-8


    Now it should be fine to apply the diff patch:

    git apply diff.patch