Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to add a comment to a diff file (unified)?

Tags:

git

git-diff

diff

I wonder if it's possible to add a certain amount of unparsed content to a diff file (unified) that is ignored as a comment.

One good use of this would be having git diffs augmented with important information such as from which branch is that diff from (especially when using the --full-index option, which merely displays the blob references).

like image 979
fstab Avatar asked Sep 24 '13 10:09

fstab


2 Answers

The unified diff starts with two line header:

 --- from-file from-file-modification-time  +++ to-file to-file-modification-time 

Anything before this header is ignored, so you can add any comment here, for example:

 This may be some useful description of this patch that  will be ignored by the diff/patch utility.  --- a/foo  2002-02-21 23:30:39.942229878 -0800  +++ b/foo  2002-02-21 23:30:50.442260588 -0800  @@ -1,7 +1,6 @@  -The Way that can be told of is not the eternal Way;  -The name that can be named is not the eternal name.   The Nameless is the origin of Heaven and Earth;  -The Named is the mother of all things.  +The named is the mother of all things.  +   Therefore let there always be non-being,     so we may see their subtlety,   And let there always be being, 

Git itself uses this space before header for some metadata, for example:

diff --git a/foo b/foo index 59a4d1f..e48dfe7 100644 --- a/foo +++ b/foo 
like image 185
Jakub Jirutka Avatar answered Sep 20 '22 19:09

Jakub Jirutka


You can use # instead of - or + in the diff file for inline comments (recognizable by GNU patch), or add a comment at the begining mentioned by @JakubJirutka

 This may be some useful description of this patch that  will be ignored by the diff/patch utility.  --- a/foo  2002-02-21 23:30:39.942229878 -0800  +++ b/foo  2002-02-21 23:30:50.442260588 -0800  @@ -1,7 +1,6 @@  # comments on the deletion of the two lines  -The Way that can be told of is not the eternal Way;  -The name that can be named is not the eternal name.   The Nameless is the origin of Heaven and Earth;  -The Named is the mother of all things.  +The named is the mother of all things.  +   Therefore let there always be non-being,     so we may see their subtlety,   And let there always be being, 
like image 34
xuhdev Avatar answered Sep 21 '22 19:09

xuhdev