1

まず、私は Python 3.3 に取り組んでいることをお伝えしなければなりません。

私はpythonと正規表現から始めています。2つの正規表現の間で.txtファイルからテキストブロックをキャッチする方法を知りたいです。

私のファイルにあるものの例を次に示します。

    commit 1f0883381054b796b643dcff974435633eed8a79
    this is
    commit 1
    bloc

    commit 2f0883381054b796b643dcff974435633eed8a78
    this is
    commit 2

    bloc

    commit 3f0883381054b796b643dcff974435633eed8a77
    this is

    commit 3


    bloc

    commit 4f0883381054b796b643dcff974435633eed8a76

    this is
    commit 
    4
    bloc

commitしたがって、 AND で始まり、スペースと 40文字が続く2 つの正規表現の間のテキストをキャッチしたいと思います (これは : だと思います^commit.{41})。注: 開始したばかりでcommit後ろに何もない行は機能しません。そしてもちろん、最後のコミット ブロックを取得できる必要があります。a で終わるのではなくcommit.{41}、ファイルの最後で終わります。

すべてのブロックができたら、それに取り組むことができなければなりません。これはどのようにgit log -p見えるかです

commit 1f0883381054b796b643dcff974435633eed8a79
Merge: 4e1d5f7 8ffg9do
Author: name <email>
Date:   date of commit

    "comment 
    of the commit
    on multilines"

diff --…
index …
--- …
+++ …
@@ …
-…
+…
 …

diff --…
index …
--- …
+++ …
@@ …

commit 2f0883381054b796b643dcff974435633eed8a78
Author: name <email>
Date:   date

たとえば、取得しcommitBlock[0]ます。それは次のようになります。

Merge: 4e1d5f7 8ffg9do
Author: name <email>
Date:   date of commit

    "comment 
    of the commit
    on multilines"

diff --…
index …
--- …
+++ …
@@ …
-…
+…
 …

diff --…
index …
--- …
+++ …
@@ …

行を抽出Author:(= Author: name <email>)

のコメントを抽出しcommitBlockます:「commit of the commit on multilines」

についても同じですdiffBlock

注: と同様にcommitBlock、 は、別のまたは またはがdiffBlockある場合は停止する必要があります。diffcommit.{41}end of file


私はいくつかのことを試しました:

マルチネスブロックを取得する必要があることに気付く前に、これが私が持っていたものです。

source = open("file.txt","rt", encoding="ISO-8859-1")

for line in source:

    commit = re.findall('^commit.{41}',line) #line starts with by "commit" and is followed by 41 characters
    merge = re.findall('^Merge:.*',line)
    author = re.findall('^Author:.*',line)
    date = re.findall('^Date:.*',line)
    signed = re.findall('Signed-off-by:.*', line)

    for commitLine in commit:
        print (commitLine)
        #post into DB

    for mergeLine in merge:
        print (mergeLine)
        #post into DB
    .
    .
    .

または re.findall('^commit.{41}(.*?)^commit.{41}| endoffile ', source.read(), re.DOTALL|re.M)

も使ってみましたre.split()。それはうまく機能しcommitBlockます!diffBlockしかし、分割したいときに問題があり、ブロックを停止するcommentBlockために時々行を使用する必要があるためです。commitそして、それはもう表示されませんsplit

import os
import re
from pymongo import Connection


source = open("testSelection.txt","r", encoding="ISO-8859-1") #file that we want to analyse
sourceRead = open("testSelection.txt","r", encoding="ISO-8859-1").read() #writing source.read() bugs...
print(source.name)


commit = []
for line in source:
    if line[:6]=="commit":
            commitId = line[7:]
            commit.append(line)

f=1
while f < len(commitBlock):
    lineCommitBlock = re.split('\n', commitBlock[f])
    diffBlock = re.split('\ndiff', commitBlock[f])
    print("-----------------------------NEW COMMIT BLOCK-------------------------------")
    print(commit[f-1])
    i=0
    while i < len(lineCommitBlock):
        if "Merge:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        elif "Author:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        elif "Date:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        elif "Signed-off-by:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        elif "Tested-by:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        elif "Reviewed-by:" in lineCommitBlock[i]:
            print(lineCommitBlock[i])
        i += 1
    print("Before commentBlock <--------------------------------------------------------------------")

    print("Adter commentBlock <--------------------------------------------------------------------")
    j=1   
    while j < len(diffBlock):
        print(diffBlock[j])
        j += 1

    f += 1

source.close()

(私は同意します、それは混乱しているように見えます!)

私の問題を解決する方法はありますか?ありがとう !


編集:
私はほとんど仕事を終えたので、GitPython(3ではなくPython 2で動作します...)を使用するよりも、正規表現または他の何かでcommentBlockをキャッチする方法を見つける方が速いと思います。
誰かがこの問題を手伝ってくれますか?

Date:必要なのは、adiffまたは acommitまたはファイルの終わりで始まる行の後のテキストをキャッチすることだけです。

私は本当にこの点で立ち往生しています...

4

0 に答える 0