-
Notifications
You must be signed in to change notification settings - Fork 47
/
git.m
151 lines (144 loc) · 4.83 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function [cmdout, statout] = git(varargin)
% A thin MATLAB wrapper for Git.
%
% Short instructions:
% Use this exactly as you would use the OS command-line verison of Git.
%
% Long instructions are:
% This is not meant to be a comprehensive guide to the near-omnipotent
% Git SCM:
% http://git-scm.com/documentation
%
% Common MATLAB workflow:
%
% % Creates initial repository tracking all files under some root
% % folder
% >> cd ~/
% >> git init
%
% % Shows changes made to all files in repo (none so far)
% >> git status
%
% % Create a new file and add some code
% >> edit foo.m
%
% % Check repo status, after new file created
% >> git status
%
% % Stage/unstage files for commit
% >> git add foo.m % Add file to repo or to stage
% >> git reset HEAD . % To unstage your files from current commit area
%
% % Commit your changes to a new branch, with comments
% >> git commit -m 'Created new file, foo.m'
%
% % Other useful commands (replace ellipses with appropriate args)
% >> git checkout ... % To restore files to last commit
% >> git branch ... % To create or move to another branch
% >> git diff ... % See line-by-line changes
%
% Useful resources:
% 1. GitX: A visual interface for Git on the OS X client
% 2. Github.com: Remote hosting for Git repos
% 3. Git on Wikipedia: Further reading
%
% v0.1, 27 October 2010 -- MR: Initial support for OS X & Linux,
% untested on PCs, but expected to work
%
% v0.2, 11 March 2011 -- TH: Support for PCs
%
% v0.3, 12 March 2011 -- MR: Fixed man pages hang bug using redirection
%
% v0.4, 20 November 2013-- TN: Searching for git in default directories,
% returning results as variable
%
% v0.5, 22 January 2015 -- TP: Suppressed printing of ans before
% git command output
%
% v0.6, 26 January 2015 -- HG: Add path to git
%
% v0.7 09 April 2015 -- VF: If user requests it, return Git results
% as a variable instead of displaying them.
%
% v0.8 24 Juli 2015 -- MvdL: Return status output
%
%
% Contributors: (MR) Manu Raghavan
% (TH) Timothy Hansell
% (TN) Tassos Natsakis
% (TP) Tyler Parsons
% (HG) Han Geerligs
% (VF) Vadim Frolov
% (MvdL) Marcel van der Linden
%
orgpath=getenv('PATH');
quit_function=0;
try
% Test to see if git is installed
[status,~] = system('git --version');
% if git is in the path this will return a status of 0
% it will return a 1 only if the command is not found
% git command output
result = '';
if status
% Checking if git exists in the default installation folders (for
% Windows)
if ispc
search = ~isempty(dir('c:\Program Files\Git\bin\git.exe'));
searchx86 = ~isempty(dir('c:\Program Files (x86)\Git\bin\git.exe'));
else
search = 0;
searchx86 = 0;
end
if ~(search||searchx86)
% If git is NOT installed, then this should end the function.
result = sprintf('git is not installed\n%s\n',...
'Download it at http://git-scm.com/download');
quit_function=1; % set quit_function flag: only result is displayed
end
end
% if quit_function then only display message
if ~quit_function
% If git exists but the status is 1, then it means that it is
% not in the path. We should add the path
if status
if search
gitpath='c:\Program Files\Git\bin';
else if searchx86
gitpath='c:\Program Files (x86)\Git\bin';
end
end
setenv('PATH',[gitpath pathsep orgpath]); % add path to git
end
% We can call the real git with the arguments
arguments = parse(varargin{:});
if ispc
prog = '';
else
prog = ' | cat';
end
if strcmp(arguments, 'commit ')
answer = inputdlg('Comments:','Commit''s comments');
arguments = [arguments '-m"' char(answer) '"'];
end
[status,result] = system(['git ',arguments,prog]);
end
if nargout >= 1
cmdout = strtrim(result);
statout = status;
else
% Display result instead of returning it
% to suppress output of ans
disp(result);
end
% restore the original path
setenv(orgpath);
catch
% restore the original path
setenv(orgpath);
end
end
function space_delimited_list = parse(varargin)
space_delimited_list = cell2mat(...
cellfun(@(s)([s,' ']),varargin,'UniformOutput',false));
end