forked from slayton/matlab-git
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.m
103 lines (81 loc) · 2.97 KB
/
git.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
97
98
99
100
101
102
103
function git(varargin)
% GIT - use GIT from matlab, using standard GIT commands
%
% Examples:
% % initialize a repository
% >> git init;
%
% % add a file to the repository
% >> git add myFile.m % begin tracking my_function.m
%
% % commit changes to the repository
% >> git commit myFile.m -m 'initial checkin of myFile.m'
%
% % push to another repository
% >> git push origin master
%
%
% Commands that require additionall input (such as commits without -m flags)
% will generally fail unless the environment variable EDITOR is defined. However,
% a preferred text editor can be specified within Matlab using the global
% variable GIT_EDITOR (Pro-tip: include this code in your startup.m file)
% For Example:
%
% Declare the global variable
% >> global GIT_EDITOR;
%
% For linux:
% >> GIT_EDITOR = '/path/to/editor'; % provide the complete path
%
% For Mac OSX:
% >> GIT_EDITOR = 'TextEdit'; % Simply provide the application name
%
% The most up to date version of this code can be found at:
% https://github.com/slayton/matlab-git
%
% Copyright(c) 2012, Stuart P. Layton <[email protected]> MIT
% http://stuartlayton.com
% Revision History
% 2012/12/04 - Initial Release
% define EDIT_ARG based upon the OS and if GIT_EDITOR is set
global GIT_EDITOR;
if isempty(GIT_EDITOR)
EDIT_ARG = ' ';
else
if ismac
EDIT_ARG = ['export EDITOR="open -a ', GIT_EDITOR, '";'];
elseif isunix
EDIT_ARG = ['export EDITOR="', GIT_EDITOR, '";'];
%not yet supported
elseif ispc
error('Windows computers are not currently supported.');
end
end
GIT_BINARY = 'git';
args = convertToString(varargin{:});
% create the command to execute
% cat is included so that calls to diff don't get messed up in the command window
cmd = sprintf('%s %s %s | cat', EDIT_ARG, GIT_BINARY, args);
[~, result] = system(cmd);
fprintf('%s', result);
% Parse result and determine what happend
% Check to see if git is installed
if strfind(result, 'command not found')
error('%s is not installed', GIT_BINARY);
%Check to see if an invalid editor was specified
elseif strfind(result, 'error: There was a problem with the editor')
warning('%s is not a valid editor\n', GIT_EDITOR);
%Check to see if a commit message was missing
elseif strfind(result, 'Please supply the message using either -m or -F option.')
%if GIT_EDITOR isn't defined, inform the user, otherwise do nothing as
%they probably left the message empty intentionally
if isempty(GIT_EDITOR)
fprintf('\nConsider creating a global matlab variable named GIT_EDITOR\n')
fprintf('and pointing it to the binary of your preferred graphical text editor\n');
fprintf('For more information run: help git\n');
end
end
end
function argList = convertToString(varargin)
argList = cell2mat( cellfun( @(x)[ char(x), ' '] , varargin, 'UniformOutput', false ) );
end