Search code examples
python-3.xdataframegit-worktree

How to print git command output to python dataframe


I'm using the below git command output: git worktree list into streamlit table object. For example:

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

I would like to take only the valid ones (a to c) and print them to python dataframe. For example example

where worktree name is F:/demo/a and its check out branch is [dev/arielma/a]


Solution

  • Use git worktree list --porcelain and parse the output:

    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....

    Sample parsing:

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