How to create a patch for a whole directory to update it?

前端 未结 3 1985
北海茫月
北海茫月 2020-12-12 09:46

I know there are several threads on this already, but no one has fully explained exactly how to perform the initial diff to create the patch file, then how to apply

3条回答
  •  囚心锁ツ
    2020-12-12 09:55

    I needed to create a patch file and send it to someone so they could update their directory to match mine. There are many caveats with diff and patch however, so it ended up taking me hours to figure out something so conceptually simple. Absolute paths seem to be preferred over relative paths, and many of the options seem to have evolved from niche use cases. I finally figured out a solution based on David H's answer, with additional tips from Lakshmanan Ganapathy):

    • Back up your directory to directory.orig
    • Modify your directory to reach the desired state
    • Save diff from directory.orig to directory in file.patch so name matches for recipient

    Here are my notes:

    # to create patch:
    # copy  backup to something like .orig alongside it
    cp -r / /.orig
    # create/update/delete files/folders in  until desired state is reached
    # change working directory to 
    cd /
    # create patch file alongside 
    diff -Naru ../.orig . > ../file.patch
    # -N --new-file Treat absent files as empty.
    # -a --text Treat all files as text.
    # -r --recursive Recursively compare any subdirectories found.
    # -u -U NUM --unified[=NUM] Output NUM (default 3) lines of unified context.
    
    # to apply patch:
    # change working directory to 
    cd /
    patch -s -p0 < /file.patch
    # -s or --silent or --quiet Work silently, unless an error occurs.
    # -pN or --strip=N Strip smallest prefix containing num leading slashes from files.
    
    # to undo patch (note that directories created by patch must be removed manually):
    # change working directory to 
    cd /
    patch -Rs -p0 < /file.patch
    # -R or --reverse Assume that patch was created with the old and new files swapped.
    # -s or --silent or --quiet Work silently, unless an error occurs.
    # -pN or --strip=N Strip smallest prefix containing num leading slashes from files.
    

提交回复
热议问题