0

I used diff to produce a patch file. diff old.txt new.txt:

1c1
< Lorem ipsum dolor sit amet, consectetur adipiscing
---
> The quick brown fox jumps over the lazy dog.

The output is in the default "normal diff" format.

Can I write comments in this patch file? If so, will the patch file be reasonably portable?

1
  • BTW at least in my experience the traditional diff format is rarely used. Afaict it suffers from the problem of providing no context, so if the file to patch is not identical to the file that was used to generate the patch, mis-application becomes more likely. Most diff files nowadays are distributed in the "unified" format. – plugwash Nov 9 '20 at 15:45
1

patch ignores lines that don't look like diff-output until it finds a chunk which matches that syntax. This is reasonably portable: existing implementations do this. The patches which I construct (e.g., using diff-patch) for ncurses and xterm all have a preamble giving the diffstat, prefixing the lines with "#" to make this unambiguous, e.g., quoting one:

# ncurses 6.2 - patch 20200301 - Thomas E. Dickey
#
# ------------------------------------------------------------------------------
#
# Ncurses 6.2 is at
#       ftp.gnu.org:/pub/gnu
#
# Patches for ncurses 6.2 can be found at
#       ftp://ftp.invisible-island.net/ncurses/6.2
#       http://invisible-mirror.net/archives/ncurses/6.2 
#
# ------------------------------------------------------------------------------
# ftp://ftp.invisible-island.net/ncurses/6.2/ncurses-6.2-20200301.patch.gz
# patch by Thomas E. Dickey <dickey@invisible-island.net>
# created  Mon Mar  2 23:18:56 UTC 2020
# ------------------------------------------------------------------------------
# NEWS                             |    8 +++++++-
# VERSION                          |    2 +-
# dist.mk                          |    4 ++--
# ncurses/base/lib_bkgd.c          |   12 +++++++++++-
# package/debian-mingw/changelog   |    4 ++--
# package/debian-mingw64/changelog |    4 ++--
# package/debian/changelog         |    4 ++--
# package/mingw-ncurses.nsi        |    4 ++--
# package/mingw-ncurses.spec       |    2 +-
# package/ncurses.spec             |    2 +-
# package/ncursest.spec            |    2 +-
# 11 files changed, 32 insertions(+), 16 deletions(-)
# ------------------------------------------------------------------------------
Index: NEWS
Prereq:  1.3455 
--- ncurses-6.2-20200229+/NEWS  2020-02-29 23:15:52.000000000 +0000
+++ ncurses-6.2-20200301/NEWS   2020-03-02 01:55:08.000000000 +0000
@@ -26,7 +26,7 @@
 -- sale, use or other dealings in this Software without prior written        --
 -- authorization.                                                            --
 -------------------------------------------------------------------------------

On the other hand, someone could construct a new patch program which gives an error in this case. Given the thirty-year history of this program, the lack of portability would reflect on the "new" program rather than how it is currently used.

2
  • The "junk preamble" works ONLY at the beginning of the file. Otherwise, a "patch" is everything between the "---" lines. No place for comments. – waltinator Nov 16 '20 at 23:59
  • OP didn't specify. However, patch works on one file at a time, with one or more chunks (whose length is well-defined). The same script adds an Index line between files, using that well-defined sense, and comments could be inserted in the same way. (patch would be confused by inserting text between chunks, but your comment isn't making that distinction). – Thomas Dickey Nov 17 '20 at 1:10
-1

No. Not if you're planning to feed the diff to a program, e.g. patch.

Can you think of a comment syntax that could NEVER be part of a diff/patch flow? I cannot.

Changes should be documented elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.