どのようにして、統一-diffのスタイルのパッチのサブセットを抽出していますか?
質問
私は、パッチのサブセットを取りたいたびに、私は唯一の私が欲しいのインデックスを抽出するためのスクリプトを記述することを強制しています。
例えば。私は、サブディレクトリに適用されたパッチを持っています 'イェーイ' と 'foo' でます。
新しいパッチを作成したり、パッチのサブセットのみを適用する方法はありますか?すなわち、唯一のサブディレクトリ「イェーイ」の下にあるすべてのインデックスをとり、既存のパッチから新しいパッチを作成します。 OR「foo」でのサブディレクトリの下にないすべてのインデックス
私が持っている場合のように、パッチ(以下擬似パッチを言い訳):
Index : foo/bar
yada
yada
- asdf
+ jkl
yada
yada
Index : foo/bah
blah
blah
- 28
+ 29
blah
blah
blah
Index : yay/team
go
huskies
- happy happy
+ joy joy
cougars
suck
どのようにして抽出したりのような唯一の「イェーイ」サブディレクトリを適用することができます:
Index : yay/team
go
huskies
- happy happy
+ joy joy
cougars
suck
もし私のスクリプトまで私は、車輪を再発明することになります解決策を... この私が知っています
解決
patchutilsするの一部であるfilterdiff
ユーティリティ、見てみましょう。
たとえば、次のパッチを持っている場合:
$ cat example.patch
diff -Naur orig/a/bar new/a/bar
--- orig/a/bar 2009-12-02 12:41:38.353745751 -0800
+++ new/a/bar 2009-12-02 12:42:17.845745951 -0800
@@ -1,3 +1,3 @@
4
-5
+e
6
diff -Naur orig/a/foo new/a/foo
--- orig/a/foo 2009-12-02 12:41:32.845745768 -0800
+++ new/a/foo 2009-12-02 12:42:25.697995617 -0800
@@ -1,3 +1,3 @@
1
2
-3
+c
diff -Naur orig/b/baz new/b/baz
--- orig/b/baz 2009-12-02 12:41:42.993745756 -0800
+++ new/b/baz 2009-12-02 12:42:37.585745735 -0800
@@ -1,3 +1,3 @@
-7
+z
8
9
次に、あなたはこのようなa
ディレクトリに唯一のもののためのパッチを抽出するために、次のコマンドを実行することができます:
$ cat example.patch | filterdiff -i 'new/a/*'
--- orig/a/bar 2009-12-02 12:41:38.353745751 -0800
+++ new/a/bar 2009-12-02 12:42:17.845745951 -0800
@@ -1,3 +1,3 @@
4
-5
+e
6
--- orig/a/foo 2009-12-02 12:41:32.845745768 -0800
+++ new/a/foo 2009-12-02 12:42:25.697995617 -0800
@@ -1,3 +1,3 @@
1
2
-3
+c
他のヒント
ここに私の迅速かつ汚いPerlのソリューションです。
perl -ne '@a = split /^Index :/m, join "", <>; END { for(@a) {print "Index :", $_ if (m, yay/team,)}}' < foo.patch
コメントでsigjuiceの要求に応答して、私は私のスクリプトソリューションを掲示しています。それは100%の弾丸の証拠はない、と私はおそらく代わりにfilterdiff
を使用します。
base_usage_str=r'''
python %prog index_regex patch_file
description:
Extracts all indices from a patch-file matching 'index_regex'
e.g.
python %prog '^evc_lib' p.patch > evc_lib_p.patch
Will extract all indices which begin with evc_lib.
-or-
python %prog '^(?!evc_lib)' p.patch > not_evc_lib_p.patch
Will extract all indices which do *not* begin with evc_lib.
authors:
Ross Rogers, 2009.04.02
'''
import re,os,sys
from optparse import OptionParser
def main():
parser = OptionParser(usage=base_usage_str)
(options, args) = parser.parse_args(args=sys.argv[1:])
if len(args) != 2:
parser.print_help()
if len(args) == 0:
sys.exit(0)
else:
sys.exit(1)
(index_regex,patch_file) = args
sys.stderr.write('Extracting patches for indices found by regex:%s\n'%index_regex)
#print 'user_regex',index_regex
user_index_match_regex = re.compile(index_regex)
# Index: verification/ring_td_cte/tests/mmio_wr_td_target.e
# --- sw/cfg/foo.xml 2009-04-30 17:59:11 -07:00
# +++ sw/cfg/foo.xml 2009-05-11 09:26:58 -07:00
index_cre = re.compile(r'''(?:(?<=^)|(?<=\n))(--- (?:.*\n){2,}?(?![ @\+\-]))''')
patch_file = open(patch_file,'r')
all_patch_sets = index_cre.findall(patch_file.read())
patch_file.close()
for file_edit in all_patch_sets:
# extract index subset
index_path = re.compile('\+\+\+ (?P<index>[\w_\-/\.]+)').search(file_edit).group('index').strip()
if user_index_match_regex.search(index_path):
sys.stderr.write("Index regex matched index: "+index_path+"\n")
print file_edit,
if __name__ == '__main__':
main()
所属していません StackOverflow