Skip to contentSkip to conversation

maint: Tell git that *.py files should use python diff hunks

PATCH In review Eric Blake 3 participants 3 messages Feb 15, 2021 – Feb 16, 2021

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 maint: Tell git that *.py files should use python diff hunks

Git's default hunk pattern recognizer favors the C language, but it also includes several built-in diff styles that give saner results in other languages. In particular, telling git to treat all .py files as python changes the beginning of diff hunks as follows:

|  --- a/python/qemu/machine.py
|  +++ b/python/qemu/machine.py
| -@@ -337,12 +337,12 @@ class QEMUMachine:
| +@@ -337,12 +337,12 @@ def _post_shutdown(self) -> None:
|               self._qmp.close()

which makes it much easier to tell what function a patch is touching, rather than a non-descript listing of what class contains the changes.

Sadly, our python files that don't use .py suffix (such as numerous iotests) do not benefit from this glob.

---
 .gitattributes | 1 +
 1 file changed, 1 insertion(+)
.gitattributes +1 -0
index 3d2fe2ecda8b..3e03ee01c0d9 100644
@@ -1,2 +1,3 @@
 *.c.inc         diff=c
 *.h.inc         diff=c
+*.py            diff=python
Signature · 1 line
2.30.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Eric Blake

Git's default hunk pattern recognizer favors the C language, but it also includes several built-in diff styles that give saner results in other languages. In particular, telling git to treat all .py files as python changes the beginning of diff hunks as follows:

As a language recommendation, "gives [improved|better|more readable] results".

Quoted 6 lines
|  --- a/python/qemu/machine.py
|  +++ b/python/qemu/machine.py
| -@@ -337,12 +337,12 @@ class QEMUMachine:
| +@@ -337,12 +337,12 @@ def _post_shutdown(self) -> None:
|               self._qmp.close()

Much better.

Quoted 6 lines

which makes it much easier to tell what function a patch is touching, rather than a non-descript listing of what class contains the changes.

Sadly, our python files that don't use .py suffix (such as numerous iotests) do not benefit from this glob.

Probably not a big deal, since those generally don't have class-based code anyway.

Quoted 2 lines
Quoted 13 lines
---
  .gitattributes | 1 +
  1 file changed, 1 insertion(+)
.gitattributes +1 -0
index 3d2fe2ecda8b..3e03ee01c0d9 100644
@@ -1,2 +1,3 @@
  *.c.inc         diff=c
  *.h.inc         diff=c
+*.py            diff=python
Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Blake
Quoted 14 lines · Eric Blake

Git's default hunk pattern recognizer favors the C language, but it also includes several built-in diff styles that give saner results in other languages. In particular, telling git to treat all .py files as python changes the beginning of diff hunks as follows:

|  --- a/python/qemu/machine.py
|  +++ b/python/qemu/machine.py
| -@@ -337,12 +337,12 @@ class QEMUMachine:
| +@@ -337,12 +337,12 @@ def _post_shutdown(self) -> None:
|               self._qmp.close()

which makes it much easier to tell what function a patch is touching, rather than a non-descript listing of what class contains the changes.

Quoted 9 lines

Sadly, our python files that don't use .py suffix (such as numerous iotests) do not benefit from this glob.

---
 .gitattributes | 1 +
 1 file changed, 1 insertion(+)

Cool stuff! I'm queueing this, if you don't mind.