将git日志文件名解析为json

时间:2015-12-02 09:46:17

标签: json git bash shell parsing

我使用以下代码获取上次提交中更改的文件的名称:

git log -1 --stat

现在我想将结果解析为JSON。我知道我可以使用漂亮的格式来解析所有' git log'将数据转换为JSON(pretty-formats),如下所示:

git log \
--pretty=format:'{%n  "commit": "%H",%n  "author": "%an <%ae>",%n  "date": "%ad",%n  "message": "%f"%n},' \
$@ | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/'

但是有没有办法解析这个JSON,例如更改文件名

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

function getcommit { \
    git show --pretty="format:"  --name-only $1 | \
    perl -pe's/^\n//g;' | \
    sed 's/\(.*\)/"\1"/g' | \
    perl -0pe 's/\n(?!\Z)/,\n/g'; \
}

export -f getcommit

git log -1 --pretty=format:'{%n  "commit": "%H",%n  "author": "%an <%ae>",%n  "date": "%ad",%n  "message": "%f",%n  "files": [ COMMIT_HASH_%H  ]%n},' | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/;s/COMMIT_HASH_(\w+)/`echo -n "";getcommit $1`/e'

基本上,我在哈希本身之前用固定字符串COMMIT_HASH_替换了提交哈希值,然后将此哈希值替换为git show --pretty="format:" --name-only $COMMIT_HASH的结果。

所有更改的文件都放入json数组&#34;文件&#34;。这适用于最后的X提交

以下是最后一次提交的示例:

[{
  "commit": "1edcef90b42afee11fbd31dcc458ae0f15a3bb6e",
  "author": "Bertrand Martel <kiruazoldik92@gmail.com>",
  "date": "Tue Oct 13 17:35:34 2015 +0200",
  "message": "update-readme",
  "files": [ "README.md",
"device.png",
"screenshot.png"
  ]
},
{
  "commit": "8aa2ce64e58b770122a3561b8ef41d807ce36abc",
  "author": "Bertrand Martel <kiruazoldik92@gmail.com>",
  "date": "Mon Oct 12 19:36:18 2015 +0200",
  "message": "fix-async-bluetooth-command-bug-bluetoooth-state-check",
  "files": [ "android/app/src/main/java/fr/bmartel/android/bluetooth/BluetoothCustomManager.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/GattTask.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/GattUtils.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/IBluetoothCustomManager.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/IBluetoothManagerEventListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/ICharacteristicListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/IDevice.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/IDeviceInitListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/IScanListListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/BluetoothDeviceAbstr.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/BluetoothDeviceConn.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/IBluetoothDeviceConn.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/listener/IPushListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/INottiDevice.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/INottiListener.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/NottiDevice.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/ActionFilterGatt.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/ISharedActivity.java",
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/LeDeviceListAdapter.java",
"android/app/src/main/java/fr/bmartel/android/notti/NottiActivity.java",
"android/app/src/main/java/fr/bmartel/android/notti/NottiBtService.java",
"android/app/src/main/java/fr/bmartel/android/notti/NottiDeviceActivity.java"
  ]
}]

这是一个脚本,它在参数中获取提交索引并返回包含已更改文件的json信息:https://gist.github.com/akinaru/a4ed5d76562e74d77282

答案 1 :(得分:1)

代替困难的正则表达式和自定义 Bash 函数,您只需使用 (真正的 JSON 解析器)从 1 个 git-command 创建 JSON 数组。

为了说明,我将从 FFmpeg repo 中获取最新的 3 次提交。

$ git log -3 --pretty=format:'%H%n%an <%ae>%n%ad%n%f' --name-only
cf12a478b206cd107343827426a05aedb83816bc
Thilo Borgmann <thilo.borgmann@mail.de>
Sun Jun 6 15:15:50 2021 +0200
fftools-cmdutils.c-Add-cmd-line-option-to-override-detection-of-cpu-count
doc/fftools-common-opts.texi
fftools/cmdutils.c
fftools/cmdutils.h

87951dcbe775b349a671b9ac2e6ac5c38aee0e79
Thilo Borgmann <thilo.borgmann@mail.de>
Sun Jun 6 15:15:00 2021 +0200
lavu-cpu.c-Add-av_force_cpu_count-to-override-auto-detection
libavutil/cpu.c
libavutil/cpu.h

b7266302a40ba48fea7a5644f08623159b3dcac7
Keyun Tong <ktong@fb.com>
Sun Jun 20 21:42:29 2021 +0200
fftools-ffmpeg-Add-new-variant-source_no_drop-to-the-force_key_frames-option
doc/ffmpeg.texi
fftools/ffmpeg.c
fftools/ffmpeg.h

$ git log -3 --pretty=format:'%H%n%an <%ae>%n%ad%n%f' --name-only | xidel -se '
  array{
    for $cmt in tokenize($raw,"\n\n")
    let $ln:=x:lines($cmt)
    return {
      "commit":$ln[1],
      "author":$ln[2],
      "date":$ln[3],
      "message":$ln[4],
      "files":array{$ln[position() = 5 to last()]}
    }
  }
'
[
  {
    "commit": "cf12a478b206cd107343827426a05aedb83816bc",
    "author": "Thilo Borgmann <thilo.borgmann@mail.de>",
    "date": "Sun Jun 6 15:15:50 2021 +0200",
    "message": "fftools-cmdutils.c-Add-cmd-line-option-to-override-detection-of-cpu-count",
    "files": ["doc/fftools-common-opts.texi", "fftools/cmdutils.c", "fftools/cmdutils.h"]
  },
  {
    "commit": "87951dcbe775b349a671b9ac2e6ac5c38aee0e79",
    "author": "Thilo Borgmann <thilo.borgmann@mail.de>",
    "date": "Sun Jun 6 15:15:00 2021 +0200",
    "message": "lavu-cpu.c-Add-av_force_cpu_count-to-override-auto-detection",
    "files": ["libavutil/cpu.c", "libavutil/cpu.h"]
  },
  {
    "commit": "b7266302a40ba48fea7a5644f08623159b3dcac7",
    "author": "Keyun Tong <ktong@fb.com>",
    "date": "Sun Jun 20 21:42:29 2021 +0200",
    "message": "fftools-ffmpeg-Add-new-variant-source_no_drop-to-the-force_key_frames-option",
    "files": ["doc/ffmpeg.texi", "fftools/ffmpeg.c", "fftools/ffmpeg.h"]
  }
]
  • 通过在空行上“标记化”来创建每个提交的序列。
  • 对于每次提交,创建一个变量来保存每一行的序列。
    x:lines($cmt)tokenize($cmt,'\r\n?|\n') 的简写)
  • 从对应的每一行返回 JSON 对象,其中文件列表变成了从第 nr.5 行到最后一行的数组。