1

私はALMOSTで全体git logを有効な形式に出力することに成功していますが、現在欠落しているのは、配列JSONの最後にある末尾のコンマを削除することだけです(以下の出力を参照)JSON

これまでこれを機能させるために使用してきawkたので、他の何かを使用してまったく同じ出力を取得できない限り、引き続き使用したいと思います。私はどんな提案にもオープンです!

スクリプトはgitリポジトリのルートに配置され、次のようにターミナルから実行されます。

./gitlog.sh

(リポジトリのルートにすでにcdされていることを考慮して)

スクリプト:

git log --all --no-merges --shortstat --pretty=format:'commit_hash %H commit_hash_abbreviated %h tree_hash %T tree_hash_abbreviated %t parent_hashes %P parent_hashes_abbreviated %p author_email %ae author_date %ad author_date_unix_timestamp %at author_date_iso_8601 %ai committer_email %ce' | paste -d " " - - - | tail -r | awk -v q='"' -v c=':' -v cl='{' -v cr='}' -v bl='[' -v br=']' -v e=',' -v t='    ' '
BEGIN {
    print cl;
    print t q "commits" q c bl;
}
{
    count++;
    print t t cl;
    print t t t q "commit_nr" q c q count q e;
    print t t t q "commit_hash" q c q $2 q e;
    print t t t q "commit_hash_abbreviated" q c q $4 q e;
    print t t t q "tree_hash" q c q $6 q e;
    print t t t q "tree_hash_abbreviated" q c q $8 q e;
    if ( count == 1 ) {
        print t t t q "parent_hashes" q c q "" q e;
        print t t t q "parent_hashes_abbreviated" q c q "" q e;
        print t t t q "author_email" q c q $12 q e;
        print t t t q "date_day_week" q c q $14 q e;
        print t t t q "date_month_name" q c q $15 q e;
        print t t t q "date_month_day" q c q $16 q e;
        print t t t q "date_hour" q c q $17 q e;
        print t t t q "date_year" q c q $18 q e;
        print t t t q "date_hour_gmt" q c q $19 q e;
        print t t t q "author_date_unix_timestamp" q c q $21 q e;
        print t t t q "date_iso_8601" q c q $23 q e;
        print t t t q "committer_email" q c q $27 q e;
        print t t t q "files_changed" q c q $28 q e;
        print t t t q "insertions" q c q $31 q e;
        print t t t q "deletions" q c q $33 q e;
        print t t t q "impact" q c q $31 - $33 q;
    } else {
        print t t t q "parent_hashes" q c q $10 q e;
        print t t t q "parent_hashes_abbreviated" q c q $12 q e;
        print t t t q "author_email" q c q $14 q e;
        print t t t q "date_day_week" q c q $16 q e;
        print t t t q "date_month_name" q c q $17 q e;
        print t t t q "date_month_day" q c q $18 q e;
        print t t t q "date_hour" q c q $19 q e;
        print t t t q "date_year" q c q $20 q e;
        print t t t q "date_hour_gmt" q c q $21 q e;
        print t t t q "author_date_unix_timestamp" q c q $23 q e;
        print t t t q "date_iso_8601" q c q $25 q e;
        print t t t q "committer_email" q c q $29 q e;
        print t t t q "files_changed" q c q $30 q e;
        print t t t q "insertions" q c q $33 q e;
        print t t t q "deletions" q c q $35 q e;
        print t t t q "impact" q c q $33 - $35 q;
    }
    print t t cr e;
} END {
    print t br;
    print cr;
}' > gitlog.json

出力:

例のために短縮

  {
      "commits":[
          {
              "commit_nr":"1",
              "commit_hash":"c7a397928f814f29028bccb281de60066395eaa1",
              "commit_hash_abbreviated":"c7a3979",
              "tree_hash":"e38dac0e625f63e877baa329204511ae490cd944",
              "tree_hash_abbreviated":"e38dac0",
              "parent_hashes":"",
              "parent_hashes_abbreviated":"",
              "author_email":"i@dreamyguy.com",
              "date_day_week":"Wed",
              "date_month_name":"Mar",
              "date_month_day":"6",
              "date_hour":"09:54:27",
              "date_year":"2013",
              "date_hour_gmt":"-0800",
              "author_date_unix_timestamp":"1362592467",
              "date_iso_8601":"2013-03-06",
              "committer_email":"i@dreamyguy.com",
              "files_changed":"1",
              "insertions":"4",
              "deletions":"",
              "impact":"4"
          },
          {
              "commit_nr":"2",
              "commit_hash":"ee3810c9ff8fe144c9ee58f48d99f59885f03462",
              "commit_hash_abbreviated":"ee3810c",
              "tree_hash":"c6d102d3311a32a2475ba1539247f7ecc26b23d1",
              "tree_hash_abbreviated":"c6d102d",
              "parent_hashes":"c7a397928f814f29028bccb281de60066395eaa1",
              "parent_hashes_abbreviated":"c7a3979",
              "author_email":"i@dreamyguy.com",
              "date_day_week":"Thu",
              "date_month_name":"Mar",
              "date_month_day":"7",
              "date_hour":"20:20:42",
              "date_year":"2013",
              "date_hour_gmt":"+0100",
              "author_date_unix_timestamp":"1362684042",
              "date_iso_8601":"2013-03-07",
              "committer_email":"i@dreamyguy.com",
              "files_changed":"2",
              "insertions":"481133",
              "deletions":"",
              "impact":"481133"
          },
          {
              "commit_nr":"3",
              "commit_hash":"bc9a179663f00f134041ac750a56df8280e0b50b",
              "commit_hash_abbreviated":"bc9a179",
              "tree_hash":"f80d83c9e8e14ff55b2d77921cb0479bd173bae9",
              "tree_hash_abbreviated":"f80d83c",
              "parent_hashes":"ee3810c9ff8fe144c9ee58f48d99f59885f03462",
              "parent_hashes_abbreviated":"ee3810c",
              "author_email":"i@dreamyguy.com",
              "date_day_week":"Thu",
              "date_month_name":"Mar",
              "date_month_day":"7",
              "date_hour":"20:22:03",
              "date_year":"2013",
              "date_hour_gmt":"+0100",
              "author_date_unix_timestamp":"1362684123",
              "date_iso_8601":"2013-03-07",
              "committer_email":"i@dreamyguy.com",
              "files_changed":"1",
              "insertions":"71",
              "deletions":"3",
              "impact":"68"
          },
      ]
  }

ここで問題がわかるように、行は、最後を含むループ内の各行に対して(私が名前を付けprint t t cr e;た事前定義されたawk変数を介して)コンマを出力することです。e:(

私が試したもの:

私は次のように置き換えprint t t cr e;ました:

if (getline == 0) { print t t cr; } else { print t t cr e; }

getline最後の行に到達すると戻るはずですが0、信頼できる解決策ではありません。25行の出力(25コミット)の13行目で停止します。また、公式のgitリポジトリ(https://github.com/git/git/commits/master)を使用してこのアプローチをテストしました。最後のコミット後も末尾のコンマが表示されます。

getlineスクリプトに何らかの欠陥があるため、実際の行数を取得していない可能性があります。ただし、両方とも正確な行数NRcount返します。

どんな助けでも大歓迎です!

4

1 に答える 1

2

試すことができる1つの解決策は、前の行の最後ではなく次の行の最初にコンマを追加し、printf関数を使用して改行を処理することです。

スクリプト内で、awk行が最初の行ではないかどうかを確認し、コンマを追加します。

BEGIN {
    print cl;
    print t q "commits" q c bl;
}
{
    count++;
    #--------------------
    if ( count > 1 ) {
      printf "%s\n", e
    }
    #--------------------
    ...
} END {
    ...
}' > gitlog.json

そして、最後の行の印刷では、printf改行を避けたり追加したりするために使用します。

...
printf "%s%s%s", t, t, cr;
} END {
    printf "\n%s%s\n", t, br;
    print cr;
}' > gitlog.json

私はそれをテストしましたが、動作しているようです。お役に立てば幸いです。

于 2013-03-11T22:47:49.930 に答える