Skip to content

Commit 0a532ab

Browse files
committed
fix removal of branches
1 parent dd99406 commit 0a532ab

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

src/checkRemoteBranchExistence.m

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function branchExists = checkRemoteBranchExistence(branchName)
2-
% Checks if a branch exists locally
1+
function [branchExists_up, branchExists_org] = checkRemoteBranchExistence(branchName)
2+
% Checks if a branch exists remotely (origin and upstream)
33
%
44
% USAGE:
55
%
@@ -13,24 +13,43 @@
1313
%
1414
% .. Author:
1515
% - Laurent Heirendt
16+
% - Agnieszka Wegrzyn allow to check both upstream and origin remotes
17+
% (with upstream being the first argument for legacy issues)
1618

1719
global gitConf
1820

19-
% remove .git from the remoteRepoURL
21+
% remove .git from the remoteRepoURL (upstream)
2022
if strcmpi(gitConf.remoteRepoURL(end-3:end), '.git')
21-
tmpRepoName = gitConf.remoteRepoURL(1:end-4);
23+
tmpRepoName_up = gitConf.remoteRepoURL(1:end-4);
2224
else
23-
tmpRepoName = gitConf.remoteRepoURL;
25+
tmpRepoName_up = gitConf.remoteRepoURL;
26+
end
27+
28+
% remove .git from the forkURL (origin)
29+
if strcmpi(gitConf.forkURL(end-3:end), '.git')
30+
tmpRepoName_org = gitConf.forkURL(1:end-4);
31+
else
32+
tmpRepoName_org = gitConf.forkURL;
2433
end
2534

2635
% retrieve a list of all the branches
27-
[status_curl, result_curl] = system(['curl -s -k --head ' tmpRepoName '/tree/' branchName]);
28-
29-
if status_curl == 0 && ~isempty(strfind(result_curl, '200 OK'))
30-
printMsg(mfilename, ['The branch <' branchName '> exists remotely.']);
31-
branchExists = true;
36+
[status_curl_up, result_curl_up] = system(['curl -s -k --head ' tmpRepoName_up '/tree/' branchName]);
37+
38+
[status_curl_org, result_curl_org] = system(['curl -s -k --head ' tmpRepoName_org '/tree/' branchName]);
39+
40+
if status_curl_up == 0 && contains(result_curl_up, '200 OK')
41+
printMsg(mfilename, ['The branch <' branchName '> exists remotely on upstream.']);
42+
branchExists_up = true;
43+
else
44+
printMsg(mfilename, ['The remote <' branchName '> does not exist remotely on upstream.']);
45+
branchExists_up = false;
46+
end
47+
48+
if status_curl_org == 0 && contains(result_curl_org, '200 OK')
49+
printMsg(mfilename, ['The branch <' branchName '> exists remotely on origin.']);
50+
branchExists_org = true;
3251
else
33-
printMsg(mfilename, ['The remote <' branchName '> does not exist remotely.']);
34-
branchExists = false;
52+
printMsg(mfilename, ['The remote <' branchName '> does not exist remotely on origin.']);
53+
branchExists_org = false;
3554
end
3655
end

src/deleteContribution.m

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function deleteContribution(branchName)
1010
%
1111
% .. Author:
1212
% - Laurent Heirendt
13+
% - Agnieszka Wegrzyn fix upstream/origin branch removal
1314

1415
global gitConf
1516
global gitCmd
@@ -74,18 +75,50 @@ function deleteContribution(branchName)
7475
end
7576

7677
% delete the remote branch
77-
if checkRemoteBranchExistence(branchName)
78+
[branchExists_up, branchExists_org] = checkRemoteBranchExistence(branchName);
79+
80+
if branchExists_org
81+
reply = '';
7882

79-
[status_gitPush, result_gitPush] = system(['git push origin --delete ', branchName]);
83+
while isempty(reply) || ~strcmpi(reply, 'yes')
8084

81-
if status_gitPush == 0
82-
fprintf([gitCmd.lead, originCall, 'The remote <', branchName, '> branch has been deleted.', gitCmd.success, gitCmd.trail]);
83-
else
84-
fprintf(result_gitPush);
85-
error([gitCmd.lead, ' [', mfilename,'] The remote <', branchName,'> branch could not be deleted.', gitCmd.fail]);
85+
reply = input([gitCmd.lead, originCall, 'Are you sure that you want to delete the branch <', branchName, '> on ORIGIN? YES/NO [NO]: '], 's');
86+
87+
if strcmpi(reply, 'yes') % users MUST enter 'yes', not only 'y'
88+
[status_gitPush, result_gitPush] = system(['git push origin --delete ', branchName]);
89+
90+
if status_gitPush == 0
91+
fprintf([gitCmd.lead, originCall, 'The remote (origin) <', branchName, '> branch has been deleted.', gitCmd.success, gitCmd.trail]);
92+
else
93+
fprintf(result_gitPush);
94+
error([gitCmd.lead, ' [', mfilename,'] The remote (origin) <', branchName,'> branch could not be deleted.', gitCmd.fail]);
95+
end
96+
end
97+
end
98+
else
99+
fprintf([gitCmd.lead, originCall, 'The remote (origin) <', branchName,'> branch does not exist.', gitCmd.fail, gitCmd.trail]);
100+
end
101+
102+
if branchExists_up
103+
reply = '';
104+
105+
while isempty(reply) || ~strcmpi(reply, 'yes')
106+
107+
reply = input([gitCmd.lead, originCall, 'Are you sure that you want to delete the branch <', branchName, '> on UPSTREAM? YES/NO [NO]: '], 's');
108+
109+
if strcmpi(reply, 'yes') % users MUST enter 'yes', not only 'y'
110+
[status_gitPush, result_gitPush] = system(['git push upstream --delete ', branchName]);
111+
112+
if status_gitPush == 0
113+
fprintf([gitCmd.lead, originCall, 'The remote (upstream) <', branchName, '> branch has been deleted.', gitCmd.success, gitCmd.trail]);
114+
else
115+
fprintf(result_gitPush);
116+
error([gitCmd.lead, ' [', mfilename,'] The remote (upstream) <', branchName,'> branch could not be deleted.', gitCmd.fail]);
117+
end
118+
end
86119
end
87120
else
88-
fprintf([gitCmd.lead, originCall, 'The remote <', branchName,'> branch does not exist.', gitCmd.fail, gitCmd.trail]);
121+
fprintf([gitCmd.lead, originCall, 'The remote (upstream) <', branchName,'> branch does not exist.', gitCmd.fail, gitCmd.trail]);
89122
end
90123
end
91124
end

0 commit comments

Comments
 (0)