0

我有两个目录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.

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

4

2 回答 2

1
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/{}' \;
于 2012-04-11T15:38:39.043 回答
0

The following should do what you want I think.

#!/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

It is just a simple, straight forward approach using several temporarily files. You might get away with some of those if you want to.

于 2012-04-12T18:37:21.940 回答