【问题标题】:How to build a list of all git commits on master? [closed]如何在 master 上构建所有 git 提交的列表? [关闭]
【发布时间】:2021-05-23 02:28:56
【问题描述】:

给定一个 git 存储库,我想按日期列出分支 origin/master 上的所有提交及其 SHA 值。实现这一目标的最简单方法是什么?

我想要的结果是 Node.js 中代表 git 存储库的对象数组,其中包含提交数据,例如

    [
      {
        date: "2020-02-02",
        sha: "03ffd2d7c3c1fdcc86f947537c6f3afa209948dd",
      },
      {
        date: "2019-03-13",
        sha: "3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95",
      },
      ...
    ]

或者只是一个逗号分隔的列表,例如

2020-02-02
03ffd2d7c3c1fdcc86f947537c6f3afa209948dd
2019-03-13
3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95
...

【问题讨论】:

    标签: node.js git git-log


    【解决方案1】:

    因为您在这里提到了 node,所以我为您的问题提供了一个完全使用 node 环境的解决方案。

    据我测试,这可能仅限于本地存储库,但我稍后会进行更多测试,并让您知道它是否也可以用于来自 github 的存储库。

    为此,您需要 gitlog 模块。 gitlog npm page

    您可以使用npm install gitlog 安装它(更多信息在上面提到的页面)。

    // You need gitlog module to get and parse the git commits
    const gitlog = require("gitlog").default ;
    
    // You can give additional field names in fields array below to get that information too.
    //You can replace `__dirname` with path to your local repository.
    const options = {
        repo : __dirname,
        fields : ["hash", "authorDate"]
    }
    
    const commits = gitlog(options) ;
    
    //logObject takes one parameter which is an array returned by gitlog() function
    const logObject = commits => {
        let log = [] ;
        commits.forEach( value => {
            const hash = value.hash ;
            const date = value.authorDate ;
            log.push({hash, date}) ;
        })
        return log ;
    }
    
    //This returns the results in an array 
    logObject(commits) ;
    
    //This returns the array in accending order
    logObject(commits).sort((first, second) => {
        return Date.parse(first.date) - Date.parse(second.date) ;
    }) ;
    
    
    //This returns the array in decending order
    logObject(commits).sort((first, second) => {
        return Date.parse(second.date) - Date.parse(first.date) ;
    }) ;
    

    【讨论】:

      【解决方案2】:

      最简单的方法是使用 git 开箱即用的功能。这是一个例子:

      git log origin/master --date-order --format=%H%n%cs
      

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 2011-06-14
        相关资源
        最近更新 更多