簡體   English   中英

git log --pretty與文件列表?

[英]git log --pretty with list of files?

我正在嘗試使用'git log --pretty = tformat'來創建xml文件日志。 但是我在獲取每個提交的文件列表時遇到問題。

例如:我使用此命令

$echo '<?xml version="1.0"?>' > out.xml
$echo '<git>' >> out.xml
$git log   --pretty=tformat:'    <commit>%n        <h1>%s</h1>%n    </commit>'  --name-only  >> out.xml
$echo '</git>'>> out.xml

輸出:

    <?xml version="1.0"?>
<git>
    <commit>
        <h1>Commit 1</h1>
    </commit>
    <commit>
        <h1>Commit 2</h1>
    </commit>
    <commit>
        <h1>Commit 3</h1>
    </commit>
</git>

我想在提交標簽的一側添加標簽和文件列表,這樣我的最終輸出就像這樣

    <?xml version="1.0"?>
    <git>
        <commit>
            <h1>Commit 1</h1>
            <list>file1</list>
        </commit>
        <commit>
            <h1>Commit 2</h1>
            <list>file2</list>
        </commit>
        <commit>
            <h1>Commit 3</h1>
            <list>file3</list>
        </commit>
    </git>  

我確實嘗試過--name-only它會列出文件但不能格式化輸出。

任何幫助將非常感激。

這可能不是你想要的,但它很容易編寫腳本。 以下是一種實施方式,效率低但功能齊全。 小心運行它:它將在你的整個歷史中運作。 將第二行更改為revlist=$(git rev-list -10 HEAD)以查看它僅適用於最后10次提交。 根據上面的示例,它還假設您希望在文件底部提交較舊的提交。 如果您更喜歡按時間順序,請在git rev-list添加--reverse標志:

#!/bin/sh
revlist=$(git rev-list HEAD)
(
    echo '<?xml version="1.0"?>'
    echo '<git>'
    for rev in $revlist
    do
            files=$(git log -1 --pretty="format:" --name-only $rev)
            echo '    <commit>\n        <h1>\c'
            echo "$(git log -1 --pretty="%s" $rev)\c"
            echo '</h1>'
            for file in $files
            do
                    echo "        <list>$file</list>"
            done
            echo '    </commit>'
    done
    echo '</git>'
) > out.xml

我設法將所有文件信息作為單個文本字段獲取,然后使用以下powershell腳本對文件進行一些小的操作(將前兩行移動到文件的末尾):(將REPONAME替換為存儲庫的名稱,可以進一步自動化。)

git --git-dir <path to repository> log --name-only --pretty=format:"]]></files>%n</commit>%n<commit>%n<repo>REPONAME</repo>%n<hash>%H</hash>%n<author>%an</author>%n<dt>%ct</dt>%n<subject><![CDATA[%s]]></subject>%n<files><![CDATA[" | Out-File REPONAME.xml
$x = get-content REPONAME.xml
@("<gitrepo>") + $x[2..$x.count] + $x[0..1] + @("</gitrepo>") | set-content REPONAME.xml

要分割文件,我必須在數據庫中進行一些進一步處理(拆分換行等),但這超出了本答案的范圍。

這會輸出一個看起來像這樣的文件:

<gitrepo>
<commit>
<repo>REPONAME</repo>
<hash>388d77493cb2b4d3260cd12b1adcf23e947755f4</hash>
<author>Some Guy</author>
<dt>1401802177</dt>
<subject><![CDATA[Comments added to files]]></subject>
<files><![CDATA[
src/aben/blanksid.w
src/aben/c_typop.w
src/aben/freg.w
src/aben/freskid.w
src/aben/grit5.w
src/aben/grokli.w

]]></files>
</commit>
</gitrepo>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM