【问题标题】:How to use git log --oneline in gitpython如何在 gitpython 中使用 git log --oneline
【发布时间】:2022-01-07 03:11:19
【问题描述】:

我正在尝试通过给出开始 sha 和结束 sha 来提取提交消息列表。在 git 中使用 git log 很容易。 但我试图通过 gitpython 库来做到这一点。 有人可以帮我实现这一目标吗?

在 Git 中的命令是这样的:

git log --oneline d3513dbb9f5..598d268f

我如何用 gitpython 做到这一点?

【问题讨论】:

    标签: gitpython


    【解决方案1】:

    GitPythonRepo.iter_commits() 函数 (docs) 支持 ref-parse-style commit ranges。所以你可以这样做:

    import git
    repo = git.Repo("/path/to/your/repo")
    commits = repo.iter_commits("d3513dbb9f5..598d268f")
    

    之后的一切都取决于您想要获得的确切格式。如果你想要类似于git log --oneline 的东西,那就行了(它是一种简化的形式,标签/分支名称没有显示):

    for commit in commits:
        print("%s %s" % (commit.hexsha, commit.message.splitlines()[0]))
    

    【讨论】:

      【解决方案2】:

      你可以使用纯gitpython:

      import git
      repo = git.Repo("/home/user/.emacs.d") # my .emacs repo just for example
      logs = repo.git.log("--oneline", "f5035ce..f63d26b")
      

      会给你:

      >>> logs
      'f63d26b Fix urxvt name to match debian repo\n571f449 Add more key for helm-org-rifle\nbea2697 Drop bm package'
      

      如果您想要漂亮的输出,请使用漂亮的打印:

      from pprint import pprint as pp
      
      >>> pp(logs)
      ('f63d26b Fix urxvt name to match debian repo\n'
       '571f449 Add more key for helm-org-rifle\n'
       'bea2697 Drop bm package')
      

      请注意 logsstr 如果您想将其列为列表,只需 使用logs.splitlines()

      Gitpython 有几乎所有与 git 相似的 API。例如,repo.git.log 代表 git logrepo.git.show 代表 git show。在Gitpython API Reference了解更多信息

      【讨论】:

        【解决方案3】:

        您可能想尝试PyDriller(GitPython 的包装器),它更容易:

        for commit in Repository("path_to_repo", from_commit="STARTING COMMIT", to_commit="ENDING_COMMIT").traverse_commits():
            print(commit.msg)
        

        如果要提交特定分支,请添加参数only_in_branch="BRANCH_NAME"。文档:http://pydriller.readthedocs.io/en/latest/

        【讨论】:

        • RepositoryMining 从何而来?你能完成你的答案吗?
        猜你喜欢
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 2021-01-30
        • 1970-01-01
        • 2017-09-18
        • 2022-11-16
        • 2021-10-26
        • 1970-01-01
        相关资源
        最近更新 更多