-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiffGitHub_pullrequest.m
76 lines (61 loc) · 2.51 KB
/
diffGitHub_pullrequest.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
function diffGitHub_pullrequest(branchname)
% Open project
proj = openProject(pwd);
% List modified models since branch diverged from main
% Use *** to search recursively for modified SLX files starting in the current folder
% git diff --name-only main..branchtomerge
gitCommand = sprintf('git --no-pager diff --name-only origin/main..origin/%s ***.slx', branchname);
[status,modifiedFiles] = system(gitCommand);
if status ~= 0
warning("git diff failed")
warning(modifiedFiles)
return;
end
modifiedFiles = split(modifiedFiles);
modifiedFiles(end) = []; % Removing last element because it is empty
if isempty(modifiedFiles)
disp('No modified models to compare.')
return
end
% Create a temporary folder to store the ancestors of the modified models
% If you have models with the same name in different folders, consider
% creating multiple folders to prevent overwriting temporary models
tempdir = fullfile(proj.RootFolder, "modelscopy");
mkdir(tempdir)
% Generate a comparison report for every modified model file
for i = 1:numel(modifiedFiles)
diffToAncestor(tempdir,string(modifiedFiles(i)));
end
% Delete the temporary folder
rmdir modelscopy s
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function report = diffToAncestor(tempdir,fileName)
ancestor = getAncestor(tempdir,fileName,lastpush);
if isempty(ancestor)
% new model - skip diff report
report = [];
return
end
% Compare models and publish results in a printable report
% Specify the format using 'pdf', 'html', or 'docx'
comp= visdiff(ancestor, fileName);
filter(comp, 'unfiltered');
report = publish(comp,'html');
end
function ancestor = getAncestor(tempdir,fileName)
[~, name, ext] = fileparts(fileName);
ancestor = fullfile(tempdir, name);
% Replace seperators to work with Git and create ancestor file name
fileName = strrep(fileName, '\', '/');
ancestor = strrep(sprintf('%s%s%s',ancestor, "_ancestor", ext), '\', '/');
% Build git command to get ancestor from main
% git show origin/main:models/modelname.slx > modelscopy/modelname_ancestor.slx
gitCommand = sprintf('git --no-pager show origin/main:%s > %s', fileName, ancestor);
[status, ~] = system(gitCommand);
if status ~= 0
% new model
ancestor = [];
end
end
% Copyright 2024-2025 The MathWorks, Inc.