用于为选定文件创建修补程序的Shell脚本

用于为选定文件创建修补程序的Shell脚本,shell,diff,patch,Shell,Diff,Patch,我有两个目录new和old,它们的目录结构几乎相似,如下所示,但有些不同 old |----1 | |-1a.cpp | |-1b.cpp |----2 | |-2c.cpp |----3 | |-3a.cpp | |-3b.cpp |----4 | |-4a.cpp | |-4b.cpp |----5 | |-5a.cpp | |-5b.cpp ------------ new |----1 | |-1a.cpp | |-1

我有两个目录
new
old
,它们的目录结构几乎相似,如下所示,但有些不同

old
|----1
|    |-1a.cpp
|    |-1b.cpp
|----2
|    |-2c.cpp
|----3
|    |-3a.cpp
|    |-3b.cpp
|----4
|    |-4a.cpp
|    |-4b.cpp
|----5
|    |-5a.cpp
|    |-5b.cpp

------------

new
|----1
|    |-1a.cpp
|    |-1b.cpp
|----4
|    |-4a.cpp
|----5
|    |-5a.cpp
|    |-5b.cpp
目录
new
包含修改过的文件。但是它保持了
old
的目录结构


如何使用
old
new
目录的
diff
实用程序编写shell脚本以生成修补程序。
补丁
应该只包含
new
目录中那些文件的差异。它不应包含
目录中的其他文件

我认为下面的内容应该是你想要的

mkpatch() {
    new=$1
    old=${1/new/old}
    patch=${1}.patch
    diff "$old" "$new" > "$patch"
}
export -f mkpatch

# cd to parent directory of old and new

find new -type f -exec bash -c 'mkpatch/{}' \;
#!/bin/sh

OLD_DIR=old
NEW_DIR=new

# Make list of files, stripping off first directory in path
find "$OLD_DIR" -type f -print | cut -d/ -f2- | sort > files-old
find "$NEW_DIR" -type f -print | cut -d/ -f2- | sort > files-new

# Get files that are common
comm -1 -2 files-old files-new > files-common

# Create list of files to diff
sed "s/^/$OLD_DIR\//" files-common > files-to-diff-old
sed "s/^/$NEW_DIR\//" files-common > files-to-diff-new

# Do the diff
paste -d ' ' files-to-diff-old files-to-diff-new | xargs -n 2 diff -u

# Cleanup. Temp file handling should probably be done better by using
# mktemp to generate file names and using trap to make sure the files
# always are deleted etc. 
rm -f files-common files-new files-old files-to-diff-new files-to-diff-old

这只是一种简单、直接的方法,使用几个临时文件。如果你愿意的话,你可能会逃脱其中一些惩罚。

恐怕不行fix@LinuxPenseur:为什么不呢?会发生什么?