私はgit log –p
、GitHub プロジェクトからの情報をデータベースに入れる必要があるプロジェクトに取り組んでいます。最初に.txtファイルに入れ、git log –p
Pythonを使用してスキャンしています。(Pythonは私にとってちょっと新しいです)。
a の構造はgit log –p
次のようになります。
commit 5f0883381054b796b643dcff974435633eed8a79
Merge: 4e1d5f7 8ffg9do
Author: name <email>
Date: date of commit
"comment of the commit"
diff --…
index …
--- …
+++ …
@@ …
-…
+…
…
diff --…
index …
--- …
+++ …
@@ …
commit 737044517f403c1080886a674845fad9c42d6bc0
Author: name <email>
Date: date of commit
completion: …
Signed-off-by: name <email>
Signed-off-by: name <email>
commit 6bf931a54fd66826f28d2808b7ad822024764d41
Merge: 727a46b 3646b1a
Author: name <email>
Date: date of commit
Merge branch …
* …:
completion: …
completion: …
completion: …
commit c3c327deeaf018e727a27f5ae88e140ff7a48595
Author: name <email>
Date: date
現時点では、正規表現を使用してコミット、マージ、作成、日付行を簡単に取得できます。
for line in source:
commit = re.findall('^commit.{41}',line)
merge = re.findall('^Merge:.*',line)
author = re.findall('^Author:.*',line)
date = re.findall('^Date:.*',line)
signed = re.findall('Signed-off-by:.*', line)
diffBlock = re.findall("^['diff''index'+-@].*", line) # bad way, I miss few lines
for commitLine in commit:
print (commitLine)
#post into DB
for mergeLine in merge:
print (mergeLine)
#post into DB
.
.
.
git log -p
しかし、コミット行を見つけるたびに分割するのが良い方法です。次に、これらのさまざまなブロックに取り組みます。また、複数行にあるコミットからコメント ブロックとすべての「差分」ブロックを取得する必要があります。
変数に入れたい2つのコミット間のテキスト全体を取得したいときに問題があります...正規表現を使用すると、^commit.{41}(.*?)^commit.{41}
(gitログの最後のコミットについて考える必要があります。 t はコミット行で終了します)。後で同じメソッドが必要になりますdiffBlock
。
いろいろ試してみたのですが…例えばこんな感じですが、うまくいきません。ブロックはほとんど見つかりませんが、すべてではありません... (それは の問題かもしれませんが"\n"
、私にはわかりません)
content = open("catchCommitBlock.txt","rt", encoding="ISO-8859-1").read() #testing file
commitBlock = re.compile("^commit.{41}(.*?)^commit.{41}",re.DOTALL|re.M)
i=0
while i < len (commitBlock.findall(content)):
print (commitBlock.findall(content)[i])
i+=1
どうすればこの問題を解決できるか分かりますか?
PS わからないことがあれば教えてください^^