하나는 통합 디프 스타일 패치 서브 세트를 어떻게 추출합니까?

StackOverflow https://stackoverflow.com/questions/710845

  •  22-08-2019
  •  | 
  •  

문제

패치의 하위 집합을 가져 가고 싶을 때마다 원하는 지수 만 추출하기 위해 스크립트를 작성해야합니다.

예를 들어 서브 디렉토리 'yay'및 'foo'에 적용되는 패치가 있습니다.

새 패치를 만들거나 패치의 하위 집합 만 적용하는 방법이 있습니까? 즉, 서브 디렉토리 'yay'아래에있는 모든 지수 만 취하는 기존 패치에서 새 패치를 만듭니다. 또는 하위 디렉토리 '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

'yay'하위 디렉토리와 같은 방법 만 추출하거나 적용 할 수있는 방법 :

Index : yay/team
 go
 huskies
- happy happy
+ joy joy
 cougars
 suck

솔루션을 스크립팅하면 바퀴를 다시 발명 할 것입니다 ...

도움이 되었습니까?

해결책

살펴보십시오 filterdiff 유틸리티의 일부입니다 Patchutils.

예를 들어, 다음 패치가있는 경우 :

$ 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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top