0

I'm using Windows 7, I have MATLAB 2015b with git version 2.6.1.windows.1. MATLAB / Git integration is working.

I have a set of MATLAB tools that I use for data analysis that are being developed with source control using Git. These tools save log files when run which give dates, times, files used, commands, and variable values. I would like to add the GIT commit SHA value to these logs so I can track the data back to the version that was ran and therefore determine if some part of the data might be invalid due to a known bug.

I know MATLAB has access to the values, I can right click on a Git controlled file and select "Source Control" and then "Show Revisions" and see the SHA. Is there a MATLAB command or accessible object method that I can use to get this value so that I can put it in my log?

Trashman
  • 1,424
  • 18
  • 27
  • 1
    Hey. Have you checked `!git hash-object ` and `!git ls-files -s ` (i.e. system commands)? – Matthias W. Aug 24 '16 at 20:13
  • Check out this thread: http://stackoverflow.com/questions/460297/git-finding-the-sha1-of-an-individual-file-in-the-index – Matthias W. Aug 24 '16 at 20:13
  • But you're probably rather looking for `!git log` or `!git rev-list` as mentioned here: http://stackoverflow.com/questions/4784575/how-do-i-find-the-most-recent-git-commit-that-modified-a-file. I'd just parse the output of the system command. – Matthias W. Aug 24 '16 at 20:18
  • @MatthiasW. Ideally, it would be using an existing internal MATLAB structure based on the Git integration without making a system call. But, your comment is definitely helpful. May be it's the only way. Unless there's a way to make ! return a value, I would have to modify it slightly to `[nil SHA] = system('git has-object ')`, then the variable SHA will have what I want. Your first suggestion of the hash-object has the data I'm looking for. I just want it simple, whatever it's current hash is will go in the log for traceability. – Trashman Aug 24 '16 at 20:24
  • I wrapped it into a function with the filename as parameter. The concatenate the system command with the filename and return the hash on success. – Matthias W. Aug 24 '16 at 20:32

1 Answers1

2

As mentioned in the comments above you can use a system call. Just wrap it in a function:

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

Save it as get_git_hashobject.m and execute it like get_git_hashobject(<filename>).

Matthias W.
  • 1,039
  • 1
  • 11
  • 23