Shannon Booth e0ae942bae system: do not treat a drive-relative path as absolute
"C:\foo" names the root of drive C, but "C:foo" names foo relative to
whatever the current directory on C happens to be. Classifying the
second as absolute means it is resolved from a root it does not name.

Rejecting absolute names was also the only thing keeping a
drive-relative name out of a patch, so is_safe_patch_path() now refuses
a colon itself. A colon never appears in a Windows file name, since
"file:stream" names an alternate data stream of "file" rather than a
file of that name.
2026-08-01 18:50:32 +02:00
2022-07-03 19:49:48 +12:00
2022-07-09 12:13:44 +12:00
2022-07-03 19:49:48 +12:00

Patch

Build codecov

Patch is an implementation of the Unix tool patch.

Goals

  • Cross-platform
  • Compatible with existing patch implementations
  • Compatible with POSIX

How to use

Generate a patch

Generate a patch file using a tool such as git or the diff utility:

# Generate a file 'file-orig.txt' with contents:
# > a
# > b
# > c
echo 'a\nb\nc\n' > file-orig.txt

# Generate a file 'file-new.txt' with contents:
# > a
# > c
echo 'a\nc\n' > file-new.txt

# Generate a unified patch to a file named 'diff.patch' showing 'file-orig.txt'
# has the line 'b' removed when compared to 'file-new.txt'.
diff -u file-orig.txt file-new.txt > diff.patch

Supported patch formats are "normal", "context", "unified" and ed scripts produced by diff -e.

Applying the patch

Run patch using the generated patch file:

patch --input diff.patch

file-orig.txt should have been patched, removing the line b to match the contents of file-new.txt.

Cross platform behavior

Patch is written to support Windows, Linux, BSD's and other Unix like systems.

Newline handling

A line that has a different line ending to another (CRLF vs LF) will be considered to not match. The flag --ignore-whitespace can be used so that lines with different line endings but the same content will be considered equal.

The flag --newline-output can be used to change how newlines will be output in patched files.

  • native: will write the entire contents of the file in native line endings (CRLF \r\n on Windows, LF \n on Unix).
  • lf: will write the file with LF \n line endings.
  • crlf: will write the file with CRLF \r\n line endings.
  • preserve: will attempt to use same line endings as lines in the file being patched.

By default, patch will select the preserve option.

Path handling

Patch will parse and apply patches using the host systems path separation semantics. For example, \ characters are treated as path separators on Windows, but as part of the file name on Unix-like systems.

Symlinks in extended-format patches are supported on Unix like systems. Symlinks are not supported on Windows, and result in the patch failing.

Build & test

Patch is built with cmake. It can be built with the following commands:

cmake -S . -B build
cmake --build build

Tests can be enabled with the -DBUILD_TESTING=On, with coverage information reported with the coverage target if -DPATCH_ENABLE_COVERAGE=On is enabled.

S
Description
DEPENDENCY: shannonbooth/patch implementation
https://brlcad.org/
Readme
628 KiB
Languages
C++ 96.6%
CMake 2.4%
Python 1%