-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitChangeCheck.m
40 lines (33 loc) · 1.48 KB
/
GitChangeCheck.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
function [ hasChanges, output ] = GitChangeCheck( )
%GITCHANGECHECK Checks whether there is uncommitted code in the pwd
% This is intended to help catch accidental temporary edits that should
% have been reverted.
%
% Uses git commands (log, status, diff) and stores the output.
% Will throw an error if git (command line) is not installed
% Active version of the code, as a git revision
[logStatusCode, headLogOutput] = system('git log HEAD -1');
assert(logStatusCode == 0, 'GitChangeCheck:gitNotInstalled', ...
['The command ''git log HEAD -1'' failed! ' ...
' Has git (command line) been installed on this machine?']);
% 'git status --porcelain' produces a short summary of what changes exist
% (i.e. files changed, added, removed...)
[~, statusOutput] = system('git status --porcelain');
hasChanges = ~isempty(statusOutput);
if hasChanges
% 'git diff' produces the current differences in the pwd
[~, diffOutput] = system('git diff');
output = sprintf(['=== Active commit ===\n'...
'%s\n'...
'=== File statuses ===\n'...
'%s\n'...
'=== Differences ===\n'...
'%s\n'],...
headLogOutput, statusOutput, diffOutput);
else
output = sprintf(['=== Active commit ===\n'...
'%s\n'...
'=== (No differences) ===\n'],...
headLogOutput);
end
end