File: GetGITInfo.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (126 lines) | stat: -rw-r--r-- 3,096 bytes parent folder | download | duplicates (2)
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
function gitInfo = GetGITInfo(directory)
% gitInfo = GetGITInfo(directory)
%
% Description:
% Retrieves the git information on a specified directory or file.  This is
% essentially a wrapper around the shell command "git".
%
% Input:
% directory (string) - Directory name of interest.
%
% Output:
% gitInfo (struct) - Structure containing the following information:
%   Path
%   Describe
%	Revision
%   LastCommit
%   RemoteRepository
%   RemoteBranch
%   LocalBranch
%
% 'gitInfo' will be empty if there is no git info for directory or if directory 
% does not exist.
%
% 7/11/13  dhb  Wrote it based on GetSVNInfo
% 7/12/13  dhb  More info, based on Ben Heasly's version of this in RenderToolbox3.
% 12/2/18  dhb  Add --no-pager to the git branch call, based on email from
%               Henryk Blasniski who says this will work better across platforms.
%               The change did not break anything obvious on my machine.
% 12/8/18  dhb  Same edit, line 99

tempFile = 'GetGITInfo_gitTemp.log';

if nargin ~= 1
    error('Usage: gitInfo = GetGITInfo(directory)');
end

gitInfo = [];
if (~exist(directory,'dir'))
    return;
end

% Look to see if we can find the git executable on the path.
gitPath = GetGitPath;
if ~exist(gitPath, 'file')
    fprintf('*** Failed to find git, returning empty.\n');
    return;
end
if IsWin
    % allow spaces in path to git
    gitPath = ['"' gitPath '"'];
end

% Get the git describe info of the specified directory.
curDir = pwd;
cd(directory);
[status, result] = system([gitPath 'git describe --always']);
cd(curDir);
if status == 0
    gitInfo.Path = directory;
    gitInfo.Describe = result(1:end-1);
else
    return;
end

% get revision number
cd(directory);
[status, result] = system([gitPath 'git rev-parse HEAD']);
cd(curDir);
if status == 0
    gitInfo.Revision = getStringLines(result);
end

% get recent commit
%   send to file, because terminal is shell is non-interactive
cd(directory);
[status] = system([gitPath 'git log --max-count=1 > ' tempFile]);
if status == 0
    fid = fopen(tempFile);
    result = char(fread(fid))';
    gitInfo.LastCommit = getStringLines(result);
    fclose(fid);
end
delete(tempFile);
cd(curDir);

% get remote repository urls
cd(directory);
[status, result] = system([gitPath 'git remote -v']);
cd(curDir);
if status == 0
    gitInfo.RemoteRepository = getStringLines(result);
end

% get remote branches
cd(directory);
[status, result] = system([gitPath 'git --no-pager branch -r']);
cd(curDir);
if status == 0
    gitInfo.RemoteBranch = getStringLines(result);
end

% get local branches
cd(directory);
[status, result] = system([gitPath 'git --no-pager branch']);
cd(curDir);
if status == 0
    gitInfo.LocalBranch = getStringLines(result);
end

end

%% Break a multi-line string into a cell array of lines.
function lines = getStringLines(string)
tokens = regexp(string, '([^\r\n]*)\r?\n?', 'tokens');
nLines = numel(tokens);
if 0 == nLines
    lines = {};
elseif 1 == nLines
    lines = tokens{1}{1};
else
    lines = cell(1, nLines);
    for ii = 1:nLines
        lines{ii} = tokens{ii}{1};
    end
end
end