{TypeError} Commit类型的对象不是JSON可序列化的

elcex8rz  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(108)

我有一个带有一些存储库信息的dict,我想把它写成一个json文件,但是在转储方法期间出现了这个错误:{TypeError} Object of type Commit is not JSON serializable.

__repo_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

repo = Repo(__repo_path)

__tags = sorted(
      (tag for tag in repo.tags if tag.commit.committed_datetime <= repo.head.commit.committed_datetime),
            key=lambda t: t.commit.committed_datetime,)

try:
    __current_branch = repo.active_branch
except TypeError as e:
    __current_branch = "release"

SCM_DATA = {
            "CHANGESET": repo.head.commit,
            "BRANCH": __current_branch,
            "TAG": __tags[-1],
            "IS_DIRTY": repo.is_dirty(),
        }

json_version = json.dumps(SCM_DATA)

我该如何修复它?

8wigbo56

8wigbo561#

确保使用名称/文本消息,而不是对象:

import git, json

repo = git.Repo('C:/data/foo')
__current_branch = repo.active_branch.name
__tags = repo.tags

SCM_DATA = {
    "CHANGESET": repo.head.commit.message,
    "BRANCH": __current_branch,
    "TAG": __tags[-1].name,
    "IS_DIRTY": repo.is_dirty(),
}

json_version = json.dumps(SCM_DATA)
print(json_version)

输出:

{'CHANGESET': 'inital commit', 'BRANCH': 'dev', 'TAG': 'v0.0.1', 'IS_DIRTY': True}

相关问题