如何将 git 命令输出打印到 python 数据帧

How to print git command output to python dataframe

我正在使用下面的 git 命令输出: git worktree list 进入 streamlit table 对象。例如:

F:/demo/a   123abc   [dev/arielma/a]
F:/demo/b   453fbd   [dev/arielma/b]
F:/demo/c   123abc   [dev/arielma/a]
F:/demo/d   3234dv   (detached HEAD)
F:/demo/e   3cxvd1   [dev/arielma/e] prunable

我只想获取有效的(a 到 c)并将它们打印到 python 数据框。例如

其中工作树名称为 F:/demo/a,其签出分支为 [dev/arielma/a]

使用git worktree list --porcelain并解析输出:

Docs:

Porcelain Format

The porcelain format has a line per attribute. Attributes are listed with a label and value separated by a single space. Boolean attributes (like bare and detached) are listed as a label only, and are present only if the value is true. Some attributes (like locked) can be listed as a label only or with a value depending upon whether a reason is available. The first attribute of a working tree is always worktree, an empty line indicates the end of the record....

示例解析:

pd.DataFrame([
    {line.split()[0]: line.split(" ",1)[1] for line in block.splitlines()}
    for block in output.split("\n\n")])