-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit_version_check.m
97 lines (80 loc) · 3.3 KB
/
git_version_check.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function [ ] = git_version_check( username, reponame, checkversion, varargin )
%[ ] = git_version_check( username, reponame, checkversion, varargin )
%
% This script pulls down information about the latest release of software
% from a given username and repo, and automatically checks to see if the
% version we are running (supplied by the user of this function) is less
% than the target release version on GitHub.
%
% Arguments:
%
% username- The owner of the repository to check against.
%
% reponame- The name of the repository to check against.
%
% checkversion- The version we'll be comparing the GitHub release version
% to. All versions must be preceeded with a 'v'.
% Examples: 'v1.0', 'v1.4.3', 'v0.1'
%
% Parameters:
%
% 'TargetVersion' - The version to check against on GitHub. If not
% specified, the script will pull down the latest version.
%
% 'WarningOnly' - If true, then no message box will appear.
%
% Versions are compared left to right, and only compared for as long as
% the TargetVersion string. So if the checkversion is 'v1.5.6', and the
% TargetVersion is 'v1.5', the software will assume the checkversion is
% higher. Conversely, if the checkversion is 'v1.5' and the TargetVersion
% is 'v1.5.6', then the checkversion will be considered obsolete.
%
%
% Robert F Cooper 07-27-2018
defaultver = 'latest';
defaultmsg = false;
p = inputParser;
p.CaseSensitive = false;
addRequired(p,'username')
addRequired(p,'reponame')
addRequired(p,'checkversion',@ischar)
addParameter(p,'TargetVersion',defaultver,@ischar)
addParameter(p,'WarningOnly',defaultmsg,@islogical)
parse(p,username,reponame,checkversion,varargin{:})
res = p.Results;
repoapisite = ['https://api.github.com/repos/' res.username '/' res.reponame '/releases/' res.TargetVersion];
try
readfromgit = webread( repoapisite );
checkvers = strsplit(checkversion(2:end), '.');
targetvers = strsplit(readfromgit.tag_name(2:end), '.');
for i=1:length(targetvers)
if i>length(checkvers)
checkver = 0;
else
checkver = str2double(checkvers{i});
end
targetver = str2double(targetvers{i});
if targetver>checkver
msgstr = [{['New version available (' readfromgit.tag_name ')! You are running ' checkversion '.']}...
{['Go to: https://www.github.com/repos/' res.username '/' res.reponame '/releases/' res.TargetVersion...
' for the latest version.']} ];
if ~res.WarningOnly
msgbox(msgstr,'New version available!')
end
warning(cell2mat(msgstr))
break;
elseif targetver<checkver
warning('Running a newer version of this code than the target version.');
break;
end
end
catch me
if strcmp(me.identifier, 'MATLAB:webservices:HTTP404StatusCodeError')
warning(['Failed to read: ' repoapisite '. Unable to connect to GitHub, OR working from a copy of the repo that does not yet have a release. YMMV!'])
elseif strcmp(me.identifier, 'MATLAB:webservices:HTTP403StatusCodeError')
warning(['Failed to read: ' repoapisite '. Unable to connect to GitHub because you''ve run the software too much!'])
else
rethrow(me);
end
end
end