Skip to content

Commit 1d1526e

Browse files
committed
fileExists() method implemented
1 parent d9dc22f commit 1d1526e

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import java.net.SocketAddress;
1212
import java.net.URI;
1313
import java.nio.charset.StandardCharsets;
14-
import java.util.*;
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.HashSet;
17+
import java.util.List;
18+
import java.util.Set;
1519

1620
import org.apache.commons.io.FileUtils;
1721
import org.apache.commons.io.IOUtils;
@@ -26,12 +30,14 @@
2630
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
2731
import org.eclipse.jgit.diff.DiffEntry.Side;
2832
import org.eclipse.jgit.diff.DiffFormatter;
29-
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
30-
import org.eclipse.jgit.errors.MissingObjectException;
31-
import org.eclipse.jgit.errors.StopWalkException;
32-
import org.eclipse.jgit.lib.*;
33-
import org.eclipse.jgit.revwalk.*;
34-
import org.eclipse.jgit.revwalk.filter.RevFilter;
33+
import org.eclipse.jgit.lib.Constants;
34+
import org.eclipse.jgit.lib.ObjectId;
35+
import org.eclipse.jgit.lib.ObjectReader;
36+
import org.eclipse.jgit.lib.Ref;
37+
import org.eclipse.jgit.lib.Repository;
38+
import org.eclipse.jgit.revwalk.RevCommit;
39+
import org.eclipse.jgit.revwalk.RevSort;
40+
import org.eclipse.jgit.revwalk.RevWalk;
3541
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
3642
import org.eclipse.jgit.transport.CredentialsProvider;
3743
import org.eclipse.jgit.transport.RefSpec;
@@ -286,33 +292,36 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
286292
public String getRepoUrl() {
287293
return repo.getRepoUrl();
288294
}
289-
290-
@Override
291-
public String getFileContent(String branchName, String fileRelativePath, String encoding) {
292-
File file;
295+
296+
private File getFileFromRepo(String branchName, String fileRelativePath, String encoding) {
293297
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
294-
Git git = getLocalGit(wc);
295-
Repository gitRepo = git.getRepository()) {
298+
Git git = getLocalGit(wc);
299+
Repository gitRepo = git.getRepository()) {
296300

297-
String bn = getRealBranchName(branchName);
298-
git
299-
.pull()
300-
.setCredentialsProvider(credentials)
301-
.call();
301+
String bn = getRealBranchName(branchName);
302+
git
303+
.pull()
304+
.setCredentialsProvider(credentials)
305+
.call();
302306

303-
git
304-
.checkout()
305-
.setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null)
306-
.addPath(fileRelativePath)
307-
.setName(bn)
308-
.call();
307+
git
308+
.checkout()
309+
.setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null)
310+
.addPath(fileRelativePath)
311+
.setName(bn)
312+
.call();
309313

310-
file = new File(wc.getFolder(), fileRelativePath);
311-
} catch (GitAPIException e) {
312-
throw new EVCSException(e);
313-
} catch (Exception e) {
314-
throw new RuntimeException(e);
315-
}
314+
return new File(wc.getFolder(), fileRelativePath);
315+
} catch (GitAPIException e) {
316+
throw new EVCSException(e);
317+
} catch (Exception e) {
318+
throw new RuntimeException(e);
319+
}
320+
}
321+
322+
@Override
323+
public String getFileContent(String branchName, String fileRelativePath, String encoding) {
324+
File file = getFileFromRepo(branchName, fileRelativePath, encoding);
316325
if (!file.exists()) {
317326
throw new EVCSFileNotFound(String.format("File %s is not found", fileRelativePath));
318327
}
@@ -657,7 +666,7 @@ public List<VCSCommit> getCommitsRange(String branchName, String startFromCommit
657666
}
658667
}
659668

660-
private RevCommit getBranchHeadCommit (String branchName) {
669+
private RevCommit getHeadRevCommit (String branchName) {
661670
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
662671
Git git = getLocalGit(wc);
663672
Repository gitRepo = git.getRepository();
@@ -681,7 +690,7 @@ private RevCommit getBranchHeadCommit (String branchName) {
681690

682691
@Override
683692
public VCSCommit getHeadCommit(String branchName) {
684-
RevCommit branchHeadCommit = getBranchHeadCommit(getRealBranchName(branchName));
693+
RevCommit branchHeadCommit = getHeadRevCommit(getRealBranchName(branchName));
685694
return new VCSCommit(branchHeadCommit.getName(), branchHeadCommit.getFullMessage(),
686695
branchHeadCommit.getAuthorIdent().getName());
687696
}
@@ -693,7 +702,6 @@ public String toString() {
693702

694703
@Override
695704
public Boolean fileExists(String branchName, String filePath) {
696-
// TODO Auto-generated method stub
697-
return null;
705+
return getFileFromRepo(branchName, filePath, StandardCharsets.UTF_8.name()).exists();
698706
}
699707
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.scm4j.vcs;
22

3-
43
import java.io.File;
54
import java.io.IOException;
65

76
import org.apache.commons.io.FileUtils;
87
import org.eclipse.jgit.api.Git;
98
import org.eclipse.jgit.lib.Repository;
109
import org.junit.After;
11-
import org.junit.Ignore;
12-
import org.junit.Test;
1310
import org.mockito.Mockito;
1411
import org.scm4j.vcs.api.IVCS;
1512
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
@@ -64,12 +61,5 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) {
6461
protected String getVCSTypeString() {
6562
return GitVCS.GIT_VCS_TYPE_STRING;
6663
}
67-
68-
@Override
69-
@Test
70-
@Ignore
71-
public void testGetCommitsRange() {
72-
73-
}
7464
}
7565

0 commit comments

Comments
 (0)