Skip to content

Commit 2f785b2

Browse files
committed
Merge remote-tracking branch 'remotes/origin/master' into Develop
# Conflicts: # src/main/java/org/scm4j/vcs/GitVCS.java
2 parents 4e62707 + 608af74 commit 2f785b2

File tree

2 files changed

+107
-82
lines changed

2 files changed

+107
-82
lines changed

src/main/java/org/scm4j/vcs/GitVCS.java

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,28 @@
11
package org.scm4j.vcs;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.File;
5-
import java.io.FileWriter;
6-
import java.io.IOException;
7-
import java.net.Authenticator;
8-
import java.net.InetSocketAddress;
9-
import java.net.PasswordAuthentication;
10-
import java.net.Proxy;
11-
import java.net.Proxy.Type;
12-
import java.net.ProxySelector;
13-
import java.net.SocketAddress;
14-
import java.net.URI;
15-
import java.nio.charset.StandardCharsets;
16-
import java.util.ArrayList;
17-
import java.util.Collections;
18-
import java.util.Comparator;
19-
import java.util.Date;
20-
import java.util.HashSet;
21-
import java.util.List;
22-
import java.util.Set;
23-
243
import org.apache.commons.io.FileUtils;
254
import org.apache.commons.io.IOUtils;
26-
import org.eclipse.jgit.api.CheckoutCommand;
5+
import org.eclipse.jgit.api.*;
276
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
28-
import org.eclipse.jgit.api.Git;
297
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
30-
import org.eclipse.jgit.api.LogCommand;
31-
import org.eclipse.jgit.api.MergeResult;
32-
import org.eclipse.jgit.api.PushCommand;
338
import org.eclipse.jgit.api.ResetCommand.ResetType;
349
import org.eclipse.jgit.api.errors.GitAPIException;
3510
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
3611
import org.eclipse.jgit.diff.DiffEntry;
3712
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
3813
import org.eclipse.jgit.diff.DiffEntry.Side;
3914
import org.eclipse.jgit.diff.DiffFormatter;
40-
import org.eclipse.jgit.lib.Constants;
41-
import org.eclipse.jgit.lib.ObjectId;
42-
import org.eclipse.jgit.lib.ObjectIdRef.Unpeeled;
43-
import org.eclipse.jgit.lib.ObjectReader;
44-
import org.eclipse.jgit.lib.Ref;
45-
import org.eclipse.jgit.lib.Repository;
15+
import org.eclipse.jgit.lib.*;
4616
import org.eclipse.jgit.revwalk.RevCommit;
4717
import org.eclipse.jgit.revwalk.RevSort;
4818
import org.eclipse.jgit.revwalk.RevTag;
4919
import org.eclipse.jgit.revwalk.RevWalk;
5020
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
5121
import org.eclipse.jgit.transport.CredentialsProvider;
52-
import org.eclipse.jgit.transport.PushResult;
5322
import org.eclipse.jgit.transport.RefSpec;
5423
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
5524
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
56-
import org.scm4j.vcs.api.IVCS;
57-
import org.scm4j.vcs.api.VCSChangeType;
58-
import org.scm4j.vcs.api.VCSCommit;
59-
import org.scm4j.vcs.api.VCSDiffEntry;
60-
import org.scm4j.vcs.api.VCSMergeResult;
61-
import org.scm4j.vcs.api.VCSTag;
62-
import org.scm4j.vcs.api.WalkDirection;
25+
import org.scm4j.vcs.api.*;
6326
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
6427
import org.scm4j.vcs.api.exceptions.EVCSException;
6528
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
@@ -68,6 +31,15 @@
6831
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
6932
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
7033

34+
import java.io.ByteArrayOutputStream;
35+
import java.io.File;
36+
import java.io.FileWriter;
37+
import java.io.IOException;
38+
import java.net.*;
39+
import java.net.Proxy.Type;
40+
import java.nio.charset.StandardCharsets;
41+
import java.util.*;
42+
7143
public class GitVCS implements IVCS {
7244

7345
public static final String GIT_VCS_TYPE_STRING = "git";
@@ -159,36 +131,33 @@ public void deleteBranch(String branchName, String commitMessage) {
159131
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
160132
Git git = getLocalGit(wc);
161133
Repository gitRepo = git.getRepository()) {
162-
try {
163134

164-
checkout(git, gitRepo, MASTER_BRANCH_NAME);
135+
checkout(git, gitRepo, MASTER_BRANCH_NAME);
165136

166137
git
167138
.branchDelete()
168139
.setBranchNames(branchName)
169-
.setForce(true) // avoid "not merged" exception
140+
.setForce(true) // avoid "not merged" exception
170141
.call();
171142

172143
RefSpec refSpec = new RefSpec( ":refs/heads/" + branchName);
173-
push(git, refSpec);
174-
} finally {
175-
git.getRepository().close();
176-
}
144+
145+
push(git, refSpec);
177146
} catch (GitAPIException e) {
178147
throw new EVCSException(e);
179148
} catch (Exception e) {
180149
throw new RuntimeException(e);
181150
}
182151
}
183152

184-
private Iterable<PushResult> push(Git git, RefSpec refSpec) throws GitAPIException {
153+
private void push(Git git, RefSpec refSpec) throws GitAPIException {
185154
PushCommand cmd = git
186155
.push()
187156
.setPushAll();
188157
if (refSpec != null) {
189158
cmd.setRefSpecs(refSpec);
190159
}
191-
return cmd
160+
cmd
192161
.setRemote("origin")
193162
.setCredentialsProvider(credentials)
194163
.call();
@@ -351,7 +320,7 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
351320
}
352321
}
353322

354-
private void checkout(Git git, Repository gitRepo, String branchName) throws Exception {
323+
void checkout(Git git, Repository gitRepo, String branchName) throws Exception {
355324
String bn = getRealBranchName(branchName);
356325
git
357326
.pull()
@@ -703,17 +672,6 @@ public List<VCSTag> getTags() {
703672
Repository gitRepo = git.getRepository();
704673
RevWalk rw = new RevWalk(gitRepo)) {
705674

706-
// git
707-
// .checkout()
708-
// .setCreateBranch(gitRepo.exactRef("refs/heads/master") == null)
709-
// .setName("master")
710-
// .call();
711-
//
712-
// git
713-
// .pull()
714-
// .setCredentialsProvider(credentials)
715-
// .call();
716-
717675
List<Ref> tagRefs = getTagRefs();
718676
List<VCSTag> res = new ArrayList<>();
719677
RevTag revTag;
@@ -733,21 +691,14 @@ public List<VCSTag> getTags() {
733691

734692
}
735693

736-
private List<Ref> getTagRefs() throws Exception {
694+
List<Ref> getTagRefs() throws Exception {
737695
return new ArrayList<>(Git
738696
.lsRemoteRepository()
739697
.setTags(true)
740698
.setHeads(false)
741699
.setRemote(repo.getRepoUrl())
742700
.setCredentialsProvider(credentials)
743701
.call());
744-
//return new ArrayList<>(Git
745-
//.lsRemoteRepository()
746-
//.setTags(true)
747-
//.setHeads(false)
748-
//.setRemote(repo.getRepoUrl())
749-
//.setCredentialsProvider(credentials)
750-
//.call());
751702
}
752703

753704
@Override

src/test/java/org/scm4j/vcs/GitVCSTest.java

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
import org.eclipse.jgit.api.Git;
55
import org.eclipse.jgit.api.errors.GitAPIException;
66
import org.eclipse.jgit.diff.DiffEntry;
7+
import org.eclipse.jgit.lib.Ref;
78
import org.eclipse.jgit.lib.Repository;
9+
import org.eclipse.jgit.revwalk.RevWalk;
810
import org.eclipse.jgit.transport.CredentialItem;
11+
import org.eclipse.jgit.transport.RefSpec;
912
import org.junit.After;
1013
import org.junit.Test;
1114
import org.mockito.Mockito;
1215
import org.mockito.exceptions.verification.WantedButNotInvoked;
1316
import org.scm4j.vcs.api.IVCS;
1417
import org.scm4j.vcs.api.VCSChangeType;
18+
import org.scm4j.vcs.api.VCSTag;
1519
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
20+
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
1621
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
1722

1823
import java.io.File;
@@ -21,6 +26,7 @@
2126
import java.lang.reflect.Method;
2227
import java.net.*;
2328
import java.nio.charset.IllegalCharsetNameException;
29+
import java.util.ArrayList;
2430
import java.util.List;
2531

2632
import static org.junit.Assert.*;
@@ -30,14 +36,16 @@ public class GitVCSTest extends VCSAbstractTest {
3036
private Repository localGitRepo;
3137
private ProxySelector proxySelectorBackup;
3238
private final RuntimeException testGitResetException = new RuntimeException("test exception on git.reset()");
39+
private GitVCS git;
3340

3441
@Override
3542
public void setUp() throws Exception {
3643
super.setUp();
37-
Git git = GitVCSUtils.createRepository(new File(localVCSWorkspace.getHomeFolder(), repoName));
38-
localGitRepo = git.getRepository();
44+
Git fileGitRepo = GitVCSUtils.createRepository(new File(localVCSWorkspace.getHomeFolder(), repoName));
45+
localGitRepo = fileGitRepo.getRepository();
3946
proxySelectorBackup = ProxySelector.getDefault();
4047
ProxySelector.setDefault(null);
48+
git = (GitVCS) vcs;
4149
}
4250

4351
@After
@@ -62,10 +70,10 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) throws Exception
6270
Git mockedGit;
6371
if (doMakeFailure) {
6472
mockedGit = Mockito.spy(((GitVCS) vcs).getLocalGit(mockedLWC));
65-
Mockito.doReturn(mockedGit).when((GitVCS) vcs).getLocalGit(mockedLWC);
73+
Mockito.doReturn(mockedGit).when(((GitVCS) vcs)).getLocalGit(mockedLWC);
6674
Mockito.doThrow(testGitResetException).when(mockedGit).reset();
6775
} else {
68-
Mockito.doCallRealMethod().when((GitVCS) vcs).getLocalGit(mockedLWC);
76+
Mockito.doCallRealMethod().when(((GitVCS) vcs)).getLocalGit(mockedLWC);
6977
mockedGit = null;
7078
}
7179
}
@@ -80,7 +88,7 @@ public void testSetCredentials() {
8088
vcs.setCredentials("user", "password");
8189
CredentialItem.Username u = new CredentialItem.Username();
8290
CredentialItem.Password p = new CredentialItem.Password();
83-
assertTrue(((GitVCS) vcs).getCredentials().get(null, u, p));
91+
assertTrue(git.getCredentials().get(null, u, p));
8492
assertEquals(u.getValue(), "user");
8593
assertEquals(new String(p.getValue()), "password");
8694
}
@@ -183,30 +191,37 @@ public void testExceptions() throws Exception {
183191
testExceptionThrowing(eCommon, m, params);
184192
}
185193
}
186-
187-
private void testExceptionThrowing(Exception testException, Method m, Object[] params) throws Exception {
188-
Mockito.reset((GitVCS) vcs);
189-
Mockito.doThrow(testException).when((GitVCS) vcs).getLocalGit(mockedLWC);
194+
195+
private void testExceptionThrowingNoMock(Exception testException, Method m, Object[] params) throws Exception {
190196
try {
191197
m.invoke(vcs, params);
192198
if (wasGetLocalGitInvoked(vcs)) {
193199
fail();
194200
}
195201
} catch (InvocationTargetException e) {
196202
if (wasGetLocalGitInvoked(vcs)) {
197-
assertTrue(e.getCause() instanceof RuntimeException);
203+
// InvocationTargetException <- EVCSException <- GitAPIException
204+
assertTrue(e.getCause().getCause().getClass().isAssignableFrom(testException.getClass()));
198205
assertTrue(e.getCause().getMessage().contains(testException.getMessage()));
199206
}
200207
} catch (Exception e) {
201208
if (wasGetLocalGitInvoked(vcs)) {
202209
fail();
203210
}
204211
}
212+
213+
}
214+
215+
private void testExceptionThrowing(Exception testException, Method m, Object[] params) throws Exception {
216+
Mockito.reset(git);
217+
Mockito.doThrow(testException).when(git).getLocalGit(mockedLWC);
218+
testExceptionThrowingNoMock(testException, m, params);
219+
205220
}
206221

207222
private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
208223
try {
209-
Mockito.verify((GitVCS) vcs).getLocalGit(mockedLWC);
224+
Mockito.verify(git).getLocalGit(mockedLWC);
210225
return true;
211226
} catch (WantedButNotInvoked e1) {
212227
return false;
@@ -217,7 +232,7 @@ private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
217232
public void testDefaultChangeTypeToVCSType() {
218233
for (DiffEntry.ChangeType ct : DiffEntry.ChangeType.values()) {
219234
if (ct != DiffEntry.ChangeType.ADD && ct != DiffEntry.ChangeType.DELETE && ct != DiffEntry.ChangeType.MODIFY) {
220-
assertEquals(((GitVCS) vcs).gitChangeTypeToVCSChangeType(ct), VCSChangeType.UNKNOWN);
235+
assertEquals(git.gitChangeTypeToVCSChangeType(ct), VCSChangeType.UNKNOWN);
221236
}
222237
}
223238
}
@@ -239,6 +254,65 @@ public void testGetFileContentExceptions() {
239254
public void testGitVCSUtilsCreation() {
240255
assertNotNull(new GitVCSUtils());
241256
}
242-
257+
258+
@Test
259+
public void testGetLastTagEmptyTagRefsList() throws Exception {
260+
Mockito.doReturn(new ArrayList<Ref>()).when(git).getTagRefs();
261+
assertNull(vcs.getLastTag());
262+
}
263+
264+
@Test
265+
public void testGetLastTagUnannotatedTag() throws Exception {
266+
createUnannotatedTag(null, TAG_NAME_1);
267+
VCSTag tag = vcs.getLastTag();
268+
assertNull(tag.getAuthor());
269+
assertNull(tag.getTagMessage());
270+
assertEquals(tag.getTagName(), TAG_NAME_1);
271+
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
272+
}
273+
274+
public void createUnannotatedTag(String branchName, String tagName) throws Exception {
275+
try (IVCSLockedWorkingCopy wc = localVCSRepo.getVCSLockedWorkingCopy();
276+
Git localGit = git.getLocalGit(wc);
277+
Repository gitRepo = localGit.getRepository();
278+
RevWalk rw = new RevWalk(gitRepo)) {
279+
280+
git.checkout(localGit, gitRepo, branchName);
281+
282+
Ref ref = localGit
283+
.tag()
284+
.setAnnotated(false)
285+
.setName(tagName)
286+
.setObjectId(null)
287+
.call();
288+
289+
localGit
290+
.push()
291+
.setPushAll()
292+
.setRefSpecs(new RefSpec(ref.getName()))
293+
.setRemote("origin")
294+
.setCredentialsProvider(git.getCredentials())
295+
.call();
296+
}
297+
}
298+
299+
@Test
300+
public void testGetLastTagExceptions() throws Exception {
301+
Ref dummyRef = Mockito.mock(Ref.class);
302+
List<Ref> refList = new ArrayList<>();
303+
refList.add(dummyRef);
304+
Mockito.doReturn(refList).when(git).getTagRefs();
305+
@SuppressWarnings("serial")
306+
GitAPIException eApi = new GitAPIException("test git exception") {};
307+
Exception eCommon = new Exception("test common exception");
308+
//Mockito.reset(git);
309+
Mockito.doThrow(eApi).when(git).getLocalGit(mockedLWC);
310+
testExceptionThrowingNoMock(eApi, vcs.getClass().getDeclaredMethod("getLastTag"), new Object[0]);
311+
312+
Mockito.reset(git);
313+
Mockito.doReturn(refList).when(git).getTagRefs();
314+
Mockito.doThrow(eCommon).when(git).getLocalGit(mockedLWC);
315+
testExceptionThrowingNoMock(eCommon, vcs.getClass().getDeclaredMethod("getLastTag"), new Object[0]);
316+
}
243317
}
244318

0 commit comments

Comments
 (0)