如何为整个目录创建补丁以进行更新?

pou*_*def 67 diff patch

我知道有这个已经几个线程,但没有一个充分说明究竟如何执行初始DIFF创建补丁文件,那么该如何应用这个补丁初始目录进行更新.

就我而言,有一个文件目录,任何人都可以从网上下载.我已经获取了该目录并对其进行了更改,并希望创建一个补丁文件,以便其他人可以将其应用于下载的目录,以准确再现我在修改后的目录中的内容.

救命?关于如何应用我的补丁,我需要告诉对方什么?

Dav*_*d H 129

我只是遇到了同样的问题 - 关于如何做到这一点的很多建议.好吧,这就是我为修补和取消修补工作所做的工作:

要创建补丁文件:

  1. 将两个目录的副本放在say/tmp中,这样我们就可以创建补丁文件,或者如果勇敢的话,将它们并排放在一个目录中.

  2. 在旧目录和新目录上运行适当的diff:

    diff -ruN orig/ new/ > file.patch
    # -r == recursive, so do subdirectories
    # -u == unified style, if your system lacks it or if recipient
    #       may not have it, use "-c"
    # -N == treat absent files as empty
    
    Run Code Online (Sandbox Code Playgroud)

如果某人拥有orig /目录,他们可以通过运行补丁重新创建新目录.

要从旧文件夹和修补程序文件重新创建新文件夹:

  1. 将补丁文件移动到orig /文件夹所在的目录

  2. 此文件夹将被破坏,因此请在某处保留备份,或使用副本.

    patch -s -p0 < file.patch
    # -s == silent except errors
    # -p0 == needed to find the proper folder
    
    Run Code Online (Sandbox Code Playgroud)
  3. 此时,orig /文件夹包含新/内容,但仍有其旧名称,因此:

    mv orig/ new/    # if the folder names are different
    
    Run Code Online (Sandbox Code Playgroud)

  • 希望我能动摇你的手.非常感谢! (5认同)
  • `patch`正在为我修补`new /`not`orig /`目录,但我找到了`-d`选项,它允许你在应用补丁之前先说'cd`到该目录然后你可以调整`-p N`相应的参数. (2认同)

Zac*_*ris 6

我需要创建一个补丁文件并将其发送给某人,以便他们可以更新他们的目录以匹配我的目录。然而,diffpatch有很多注意事项,所以最终我花了几个小时才弄清楚概念上如此简单的东西。绝对路径似乎比相对路径更受欢迎,许多选项似乎是从利基用例演变而来的。我终于根据David H 的回答找到了一个解决方案,还有来自Lakshmanan Ganapathy 的额外提示):

  • 备份你directorydirectory.orig
  • 修改您的directory以达到所需的状态
  • 从保存DIFFdirectory.origdirectoryfile.patch这样的名字对收件人匹配

以下是我的笔记:

# to create patch:
# copy <directory> backup to something like <directory>.orig alongside it
cp -r <path_to>/<directory> <path_to>/<directory>.orig
# create/update/delete files/folders in <directory> until desired state is reached
# change working directory to <directory>
cd <path_to>/<directory>
# create patch file alongside <directory>
diff -Naru ../<directory>.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 <directory>
cd <path_to>/<directory>
patch -s -p0 < <path_to>/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 <directory>
cd <path_to>/<directory>
patch -Rs -p0 < <path_to>/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.
Run Code Online (Sandbox Code Playgroud)