微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

给定一个 git 存储库,我想提取 origin/master 上的所有提交 shas 以及日期并按日期对它们进行排序。实现这一目标的最简单方法是什么?

结果应该是这样的:

[
  {
    sha: "03ffd2d7c3c1fdcc86f947537c6f3afa209948dd",date: <timestamp in some form>,},{
    sha: "3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95",...
]

注意:它不需要是一个实际的对象数组,例如可以只是一个逗号分隔的文本文件。我只需要它可以使用 Node.js 轻松转换为对象数组。

解决方法

最简单的方法是使用 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) ;
}) ;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。