Skip to content

Commit 0403c2a

Browse files
committed
Merge pull request #13 from fralik/master
Return Git results as a variable instead of displaying them
2 parents 3d80944 + d331979 commit 0403c2a

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

git.m

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function git(varargin)
1+
function cmdout = git(varargin)
22
% A thin MATLAB wrapper for Git.
33
%
44
% Short instructions:
@@ -57,24 +57,27 @@ function git(varargin)
5757
%
5858
% v0.6, 26 January 2015 -- HG: Add path to git
5959
%
60+
% v0.7 09 April 2015 -- VF: If user requests it, return Git results
61+
% as a variable instead of displaying them.
62+
%
6063
% Contributors: (MR) Manu Raghavan
6164
% (TH) Timothy Hansell
6265
% (TN) Tassos Natsakis
6366
% (TP) Tyler Parsons
6467
% (HG) Han Geerligs
68+
% (VF) Vadim Frolov
6569
%
66-
6770
orgpath=getenv('PATH');
6871
quit_function=0;
6972
try
7073
% Test to see if git is installed
7174
[status,~] = system('git --version');
7275
% if git is in the path this will return a status of 0
7376
% it will return a 1 only if the command is not found
74-
77+
7578
% git command output
7679
result = '';
77-
80+
7881
if status
7982
% Checking if git exists in the default installation folders (for
8083
% Windows)
@@ -85,15 +88,15 @@ function git(varargin)
8588
search = 0;
8689
searchx86 = 0;
8790
end
88-
91+
8992
if ~(search||searchx86)
9093
% If git is NOT installed, then this should end the function.
9194
result = sprintf('git is not installed\n%s\n',...
9295
'Download it at http://git-scm.com/download');
9396
quit_function=1; % set quit_function flag: only result is displayed
9497
end
9598
end
96-
99+
97100
% if quit_function then only display message
98101
if ~quit_function
99102
% If git exists but the status is 1, then it means that it is
@@ -107,7 +110,7 @@ function git(varargin)
107110
end
108111
setenv('PATH',[gitpath pathsep orgpath]); % add path to git
109112
end
110-
113+
111114
% We can call the real git with the arguments
112115
arguments = parse(varargin{:});
113116
if ispc
@@ -121,10 +124,14 @@ function git(varargin)
121124
end
122125
[~,result] = system(['git ',arguments,prog]);
123126
end
124-
% Display result instead of returning it
125-
% to suppress output of ans
126-
disp(result);
127-
127+
if nargout >= 1
128+
cmdout = strtrim(result);
129+
else
130+
% Display result instead of returning it
131+
% to suppress output of ans
132+
disp(result);
133+
end
134+
128135
% restore the original path
129136
setenv(orgpath);
130137
catch

0 commit comments

Comments
 (0)