Commit eb49ca11 authored by Philipp Schäfer's avatar Philipp Schäfer
Browse files

ita_git_commit_id: now also works for a detached head state

parent f78fddea
......@@ -26,27 +26,36 @@ currentBranch = '';
%% Find git folder
gitFolder = fullfile(ita_toolbox_path, '.git');
if ~exist(gitFolder, 'dir')
ita_verbose_info([upper(mfilename()) ': No git folder found. Did you download the ITA Toolbox instead of cloning it?'])
ita_verbose_info('No git folder found. Did you download the ITA Toolbox instead of cloning it?')
%% Find current branch
%% Check HEAD
headFile = fullfile(gitFolder, 'HEAD');
if ~exist(headFile, 'file')
ita_verbose_info([upper(mfilename()) ': Cannot find git head!'])
ita_verbose_info('Cannot find git head!')
txt = txt_fileread(headFile);
numLines = numel(txt);
if numLines < 2; return; end
if numLines == 0; return; end
bDetachedHead = ~strcmp(txt{1}, 'ref:');
bValidBranch = ~bDetachedHead && numLines >= 2;
%% Get commit ID of detached HEAD
gitCommitID = txt{1};
%% Get current commit ID from branch
if ~bValidBranch; return; end
branchFile = fullfile(gitFolder, txt{2});
currentBranch = strrep(txt{2}, 'refs/heads/', '');
%% Find current commit ID
if ~exist(branchFile, 'file')
ita_verbose_info([upper(mfilename()) ': Cannot find commit of current branch!'])
ita_verbose_info('Cannot find commit of current branch!')
txt = txt_fileread(branchFile);
......@@ -55,7 +64,6 @@ if isempty(txt); return; end
gitCommitID = txt{1};
function txt = txt_fileread(filename)
fileID = fopen(filename);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment