Applying a single patch to files in multiple directories

7,682

While the given pathnames do not look quite right (especially the new tree, which someone else has changed), you can keep in mind that the patch program can be told to ignore a given number of levels of directory from the output of diff using the -p option.

When applying a patch to files in a different directory than the patch-file shows, you would cd into the directory (to cancel one part of the mismatch) and use the -p option to adjust for the cd.

Here is a short script illustrating how I would solve this (using the dry-run option of GNU patch for a simple listing):

#!/bin/sh
cd /tmp/foo
OUT=/tmp/patch.diff
rm -f $OUT
diff -u orig/l1/l2/file2patch new/l1/l2/file2patch >>$OUT
diff -u orig/l1-2/file2patch new/l1-2/file2patch >>$OUT

diffstat -p1 $OUT
cd orig
patch -p1 --dry-run <$OUT

Inputs:

$ find . -type f
./new/l1/l2/file2patch
./new/l1-2/file2patch
./orig/l1/l2/file2patch
./orig/l1-2/file2patch

Output:

$ /tmp/xx
 l1-2/file2patch  |   19 +++++++++++++------
 l1/l2/file2patch |    5 ++---
 2 files changed, 15 insertions(+), 9 deletions(-)
/tmp/foo/orig
patching file l1/l2/file2patch
patching file l1-2/file2patch

Further reading:

Share:
7,682

Related videos on Youtube

cbrad
Author by

cbrad

Updated on September 18, 2022

Comments

  • cbrad
    cbrad over 1 year

    Directory structures looks like this:

    orig/l1/l2/file2patch

    orig/l1-2/file2patch

    Diff'd with

    new/l1/l2/file2patch

    new/l1-2/file2patch

    I get the correct diff between these files and save it in a file but applying it has proved to be non-trivial.

    I've tried patch -d orig/ < patch.diff but the -d expects the files that are to be patched should live immediately in orig

    Is there some way I can have patch recur on a directory and apply the patch to the specified files that exist in the patch?

    • Jeff Schaller
      Jeff Schaller about 8 years
      Look into the -p flag to tell patch your directory structure is different
    • cbrad
      cbrad about 8 years
      @JeffSchaller I have two different directories that have two different levels.
    • Alen Milakovic
      Alen Milakovic about 8 years
      @cbrad You misunderstand what the -p flag does. Read man patch, specifically the -p option.