如何在 master 上构建所有 git 提交的列表?

How to build a list of all git commits on master?

给定一个 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
...

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

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

因为您在这里提到了 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) ;
}) ;