2012-04-11 16 views
0

以下に示すようなディレクトリ構造とほぼ同じ2つのディレクトリnewoldがあります。選択したファイルのパッチを作成するためのシェルスクリプト

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ユーティリティoldnewディレクトリを使用してパッチを生成するためのシェルスクリプトを書く方法。 patchには、newディレクトリにあるファイルのみの差分のみを含める必要があります。 oldディレクトリに他のファイルを含めてはいけません。

答えて

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/{}' \; 
+0

これは修正されないのですか? –

+0

@LinuxPenseur:どうしてですか?何が起こるのですか? –

0

以下は、私が思うようにしてください。

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

これは、いくつかの一時ファイルを使用する簡単な単純なアプローチです。あなたがしたい場合は、それらのいくつかで逃げるかもしれません。