【问题标题】:Get Git Commit / Revision SHA from within MATLAB script从 MATLAB 脚本中获取 Git Commit / Revision SHA
【发布时间】:2016-12-31 21:18:05
【问题描述】:

我使用的是 Windows 7,我有 MATLAB 2015b 和 git 版本 2.6.1.windows.1。 MATLAB / Git 集成正在运行。

我有一组用于数据分析的 MATLAB 工具,这些工具正在使用 Git 进行源代码控制开发。这些工具在运行时保存日志文件,其中给出日期、时间、使用的文件、命令和变量值​​。我想将 GIT 提交 SHA 值添加到这些日志中,以便我可以将数据跟踪回运行的版本,从而确定某些数据是否可能由于已知错误而无效。

我知道 MATLAB 可以访问这些值,我可以右键单击 Git 控制的文件并选择“Source Control”,然后选择“Show Revisions”并查看 SHA。是否有 MATLAB 命令或可访问对象方法可用于获取此值以便将其放入日志中?

【问题讨论】:

  • 嘿。您检查过!git hash-object <filename>!git ls-files -s <filename>(即系统命令)吗?
  • 但您可能更愿意寻找!git log!git rev-list,如此处所述:stackoverflow.com/questions/4784575/…。我只是解析系统命令的输出。
  • @MatthiasW。理想情况下,它将使用基于 Git 集成的现有内部 MATLAB 结构,而无需进行系统调用。但是,您的评论绝对有帮助。可能这是唯一的方法。除非有办法制作!返回一个值,我将不得不将它稍微修改为[nil SHA] = system('git has-object <filename>'),然后变量 SHA 就会有我想要的。您对哈希对象的第一个建议包含我正在寻找的数据。我只是希望它简单,无论它当前的哈希是什么,都会进入日志以进行可追溯性。
  • 我将它包装到一个函数中,并以文件名作为参数。将系统命令与文件名连接起来,并在成功时返回哈希值。

标签: git matlab sha


【解决方案1】:

如上面的 cmets 中所述,您可以使用系统调用。只需将其包装在一个函数中即可:

function hash = get_git_hashobject( filename )
%get_git_hashobject Performs a system call to `git hash-object` and returnd
%the hash value.
    command = [ 'git hash-object -- ' filename ];
    [status,hash] = system(command);
    if( status ~= 0 )
        error('Unable to get hash from file.');
    end
end

将其保存为get_git_hashobject.m 并像get_git_hashobject(<filename>) 一样执行。

【讨论】:

    猜你喜欢
    • 2018-03-04
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多