0001 function version = getToolboxVersion(toolbox,fileID,mainBranchFlag)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 toolbox=char(toolbox);
0017 fileID=char(fileID);
0018
0019 if nargin<3
0020 mainBranchFlag = false;
0021 end
0022
0023 currentPath = pwd;
0024 version = '';
0025
0026
0027 try
0028 toolboxPath = which(fileID);
0029 slashPos = getSlashPos(toolboxPath);
0030 toolboxPath = toolboxPath(1:slashPos(end));
0031
0032 D = dir(toolboxPath);
0033 while ~ismember({'.git'},{D.name})
0034 slashPos = getSlashPos(toolboxPath);
0035 toolboxPath = toolboxPath(1:slashPos(end-1));
0036 D = dir(toolboxPath);
0037 end
0038 cd(toolboxPath);
0039 catch
0040 disp([toolbox ' toolbox cannot be found'])
0041 version = 'unknown';
0042 end
0043
0044 if mainBranchFlag
0045 [~,currentBranch] = system('git rev-parse --abbrev-ref HEAD');
0046 currentBranch = strtrim(currentBranch);
0047 if any([strcmp(currentBranch, "main"), strcmp(currentBranch, "master")])
0048 cd(currentPath);
0049 error(['ERROR: ' toolbox ' not in main (or master) branch. Check-out this branch of ' toolbox ' before submitting model for Git.'])
0050 end
0051 end
0052
0053 if isempty(version)
0054 try
0055 fid = fopen([toolboxPath 'version.txt'],'r');
0056 version = fscanf(fid,'%s');
0057 fclose(fid);
0058 catch
0059
0060 try
0061 [~,version] = system('git describe --tags');
0062 version = strtrim(version);
0063 [~,commit] = system('git log -n 1 --format=%H');
0064 commit = commit(1:7);
0065
0066 if ~isempty(strfind(version,'fatal')) || ~isempty(strfind(version,commit))
0067 version = ['commit ' commit];
0068 else
0069 version = strrep(version,'v','');
0070 end
0071 catch
0072 version = 'unknown';
0073 end
0074 end
0075 end
0076 cd(currentPath);
0077 end
0078
0079 function slashPos = getSlashPos(path)
0080 slashPos = strfind(path,'\');
0081 if isempty(slashPos)
0082 slashPos = strfind(path,'/');
0083 end
0084 end