Skip to content

Commit 3ea6f38

Browse files
committed
ask to confirm default dir
1 parent e303ec9 commit 3ea6f38

File tree

2 files changed

+217
-5
lines changed

2 files changed

+217
-5
lines changed

src/initDevTools.asv

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
function initDevTools(repoName,currentDir)
2+
% Initializes the development tools (username and email are requested if not configured)
3+
%
4+
% USAGE:
5+
%
6+
% initDevTools(repoName)
7+
%
8+
% INPUT:
9+
% repoName: Name of the repository for which the devTools shall
10+
% be configured (default: `opencobra/cobratoolbox`)
11+
% .. Authors:
12+
% - Laurent Heirendt, Ronan Fleming
13+
14+
global gitConf
15+
global gitCmd
16+
global resetDevToolsFlag
17+
global DEFAULTREPONAME
18+
19+
% set the repoName if not given
20+
if ~exist('repoName', 'var')
21+
repoName = DEFAULTREPONAME;
22+
end
23+
if ~exist('currentDir', 'var')
24+
currentDir = pwd;
25+
end
26+
resetDevToolsFlag = true;
27+
28+
finishup = onCleanup(@() resetDevTools());
29+
30+
% check the system and set the configuration
31+
checkSystem(mfilename, repoName);
32+
33+
if ~isfield(gitConf, 'userName'), gitConf.userName = []; end
34+
if ~isfield(gitConf, 'localDir'), gitConf.localDir = []; end
35+
36+
% parse the remoteRepoURL
37+
sepIndices = strfind(gitConf.remoteRepoURL, '/');
38+
gitConf.remoteServerName = gitConf.remoteRepoURL(1:sepIndices(3));
39+
gitConf.remoteRepoName = gitConf.remoteRepoURL(sepIndices(4) + 1:end - 4);
40+
gitConf.remoteUserName = gitConf.remoteRepoURL(sepIndices(3) + 1:sepIndices(4) - 1);
41+
42+
% ignore case for the current fork (avoids problems for untracked files with case conflicts)
43+
[status_gitIgnoreCase, result_gitIgnoreCase] = system('git config core.ignorecase true');
44+
45+
% retrieve the user name
46+
[status_gitConfUserGet, result_gitConfUserGet] = system('git config --get user.github-username');
47+
gitConf.userName = strtrim(result_gitConfUserGet);
48+
49+
if gitConf.printLevel > 0
50+
originCall = [' [', mfilename, '] '];
51+
else
52+
originCall = '';
53+
end
54+
55+
if status_gitConfUserGet == 0 && isempty(strfind(gitConf.userName, ' '))
56+
fprintf([gitCmd.lead, originCall, 'Your Github username is: ', gitConf.userName, '. ', gitCmd.success, gitCmd.trail]);
57+
else
58+
printMsg(mfilename, 'The Github username could not be retrieved or is not valid.', [gitCmd.fail, gitCmd.trail]);
59+
60+
% request the Github username if it is not known or if the username contains whitespaces
61+
if isempty(gitConf.userName) || ~isempty(strfind(gitConf.userName, ' '))
62+
gitConf.userName = input([gitCmd.lead, originCall, ' -> Please enter your Github username: '], 's');
63+
[status_gitConfUserSet, result_gitConfUserSet] = system(['git config --global --add user.github-username "', gitConf.userName, '"']);
64+
if status_gitConfUserSet == 0
65+
fprintf([gitCmd.lead, originCall, 'Your Github username is: ', gitConf.userName, '. ', gitCmd.success, gitCmd.trail]);
66+
else
67+
fprintf(result_gitConfUserSet);
68+
error([gitCmd.lead, ' [', mfilename, '] Your Github username could not be set.', gitCmd.fail]);
69+
end
70+
end
71+
end
72+
73+
% retrieve the user's email address
74+
[status_gitConfEmailGet, result_gitConfEmailGet] = system('git config --get user.email');
75+
gitConf.userEmail = strtrim(result_gitConfEmailGet);
76+
77+
if status_gitConfEmailGet == 0
78+
fprintf([gitCmd.lead, originCall, 'Your Github email is: ', gitConf.userEmail, '. ', gitCmd.success, gitCmd.trail]);
79+
else
80+
printMsg(mfilename, 'The Github email could not be retrieved.', [gitCmd.fail, gitCmd.trail]);
81+
82+
% request the Github username
83+
if isempty(gitConf.userEmail)
84+
gitConf.userEmail = input([gitCmd.lead, originCall, ' -> Please enter your Github email: '], 's');
85+
86+
[status_gitConfEmailSet, result_gitConfEmailSet] = system(['git config --global user.email "', gitConf.userEmail, '"']);
87+
if status_gitConfEmailSet == 0
88+
fprintf([gitCmd.lead, originCall, 'Your Github email is: ', gitConf.userEmail, '. ', gitCmd.success, gitCmd.trail]);
89+
else
90+
fprintf(result_gitConfEmailSet);
91+
error([gitCmd.lead, ' [', mfilename, '] Your Github email could not be set.', gitCmd.fail]);
92+
end
93+
end
94+
end
95+
96+
% define the name of the local fork directory
97+
gitConf.forkDirName = strrep([gitConf.leadForkDirName, gitConf.remoteRepoName], '\', '\\');
98+
99+
% retrieve the directory of the fork from the local git configuration
100+
[~, result_gitConfForkDirGet] = system(['git config --get user.', gitConf.leadForkDirName, gitConf.nickName, '.path']);
101+
if ~isempty(result_gitConfForkDirGet)
102+
gitConf.fullForkDir = strtrim(result_gitConfForkDirGet);
103+
gitConf.localDir = gitConf.fullForkDir;
104+
else
105+
fprintf('%s%s%s\n','Would you like to set up current directory (', currentDir, ') as a fork directory.')
106+
gitConf.fullForkDir=currentDir;
107+
gitConf.localDir = gitConf.fullForkDir;
108+
end
109+
110+
% check if the fork exists remotely
111+
checkRemoteFork();
112+
113+
% request the local directory if the fullForkDir is not yet known
114+
if isempty(gitConf.localDir) && isempty(gitConf.fullForkDir)
115+
116+
createDir = false;
117+
118+
while ~createDir
119+
reply = input([gitCmd.lead, originCall, ' -> Please define the location of your fork\n current: ', strrep(currentDir, '\','\\'),'\n Enter the path(press ENTER to use the current path): '], 's');
120+
121+
% define the local directory as the current directory if the reply is empty
122+
if isempty(reply)
123+
gitConf.localDir = strrep(pwd, '\', '\\');
124+
else
125+
gitConf.localDir = reply;
126+
end
127+
128+
% strip the fork-nickName folder from the localDir if present
129+
if ~isempty(gitConf.localDir) && length(gitConf.forkDirName) <= length(gitConf.localDir)
130+
if strcmp(gitConf.localDir(end - length(gitConf.forkDirName) + 1:end), gitConf.forkDirName)
131+
gitConf.localDir = gitConf.localDir(1:end - length(gitConf.forkDirName));
132+
end
133+
end
134+
135+
% add a fileseparator if not included
136+
if ~strcmp(gitConf.localDir(end), filesep)
137+
gitConf.localDir = strrep([gitConf.localDir, filesep], '\', '\\');
138+
end
139+
140+
% warn the user of not using a fork-nickName directory or a git cloned directory as it will be cloned
141+
if ~isempty(strfind(gitConf.localDir, gitConf.nickName)) % contains the nickname
142+
printMsg(mfilename, ['The specified directory already contains a ', gitConf.nickName, ' copy (clone).'], gitCmd.trail);
143+
createDir = true;
144+
gitConf.localDir = gitConf.localDir(1:end - length(gitConf.nickName) - 1 - length(gitConf.leadForkDirName));
145+
146+
elseif exist([gitConf.localDir filesep '.git'], 'dir') == 7 % contains a .git folder
147+
printMsg(mfilename, ['The specified directory already is a git repository (git-tracked).'], gitCmd.trail);
148+
149+
else
150+
createDir = true;
151+
end
152+
end
153+
154+
% define the fork directory name
155+
gitConf.fullForkDir = strrep([gitConf.localDir, gitConf.forkDirName], '\', '\\');
156+
157+
if exist(gitConf.localDir, 'dir') ~= 7
158+
reply = input([gitCmd.lead, originCall, ' -> The specified directory (', gitConf.localDir, ') does not exist. Do you want to create it? Y/N [Y]:'], 's');
159+
160+
% create the directory if requested
161+
if (isempty(reply) || strcmpi(reply, 'y') || strcmpi(reply, 'yes')) && createDir
162+
system(['mkdir ', gitConf.localDir]);
163+
printMsg(mfilename, 'The directory has been created.');
164+
else
165+
error([gitCmd.lead, ' [', mfilename, '] The specified directory does not exist.', gitCmd.fail]);
166+
end
167+
end
168+
end
169+
170+
resetDevToolsFlag = false;
171+
172+
% permanently store the fork directory in the git configuration (ask the user explicitly)
173+
[status_gitConfForkDirSet, result_gitConfForkDirSet] = system(['git config --global user.', gitConf.leadForkDirName, gitConf.nickName, '.path "', gitConf.fullForkDir, '"']);
174+
if status_gitConfForkDirSet == 0
175+
fprintf([gitCmd.lead, originCall, 'Your fork directory has been set to: ', gitConf.fullForkDir, '. ', gitCmd.success, gitCmd.trail]);
176+
else
177+
fprintf(result_gitConfForkDirSet);
178+
error([gitCmd.lead, ' [', mfilename, '] Your fork directory could not be set.', gitCmd.fail]);
179+
end
180+
181+
% clone the fork
182+
freshClone = cloneFork();
183+
184+
% proceed with configuring the fork
185+
configureFork();
186+
187+
% update the fork
188+
if ~freshClone
189+
190+
% change to the local fork directory
191+
cd(gitConf.fullForkDir);
192+
193+
% retrieve the status of the git repository
194+
[status_gitStatus, result_gitStatus] = system('git status -s');
195+
196+
% only update if there are no local changes
197+
if status_gitStatus == 0 && isempty(result_gitStatus)
198+
updateFork(true);
199+
else
200+
printMsg(mfilename, 'The local fork cannot be updated as you have uncommitted changes.', [gitCmd.fail, gitCmd.trail]);
201+
end
202+
end
203+
204+
% print the current configuration
205+
fprintf([gitCmd.lead, originCall, ' -- Configuration -------- ', gitCmd.trail])
206+
fprintf([gitCmd.lead, originCall, ' GitHub username: ', gitConf.userName, gitCmd.trail]);
207+
fprintf([gitCmd.lead, originCall, ' GitHub email: ', gitConf.userEmail, gitCmd.trail]);
208+
fprintf([gitCmd.lead, originCall, ' Local directory : ', gitConf.fullForkDir, gitCmd.trail])
209+
fprintf([gitCmd.lead, originCall, ' Remote fork URL: ', gitConf.forkURL, gitCmd.trail]);
210+
fprintf([gitCmd.lead, originCall, ' Remote repository URL: ', gitConf.remoteRepoURL, gitCmd.trail]);
211+
212+
end

src/initDevTools.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ function initDevTools(repoName,currentDir)
9898

9999
% retrieve the directory of the fork from the local git configuration
100100
[~, result_gitConfForkDirGet] = system(['git config --get user.', gitConf.leadForkDirName, gitConf.nickName, '.path']);
101-
if isempty(result_gitConfForkDirGet)
102-
fprintf('%s%s%s\n','Attempting to progress by assuming the current directory (', currentDir, ') is a fork directory.')
103-
gitConf.fullForkDir=currentDir;
104-
gitConf.localDir = gitConf.fullForkDir;
105-
else
101+
if ~isempty(result_gitConfForkDirGet)
106102
gitConf.fullForkDir = strtrim(result_gitConfForkDirGet);
107103
gitConf.localDir = gitConf.fullForkDir;
104+
else
105+
fprintf('%s\n','No existing information about the location of the fork directory.')
106+
gitConf.fullForkDir = '';
107+
gitConf.localDir = '';
108108
end
109109

110110
% check if the fork exists remotely

0 commit comments

Comments
 (0)