Skip to content

Commit 6eba4cb

Browse files
committed
refactor
1 parent eae19b1 commit 6eba4cb

File tree

1 file changed

+74
-93
lines changed

1 file changed

+74
-93
lines changed

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

Lines changed: 74 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,26 @@
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.HashSet;
19-
import java.util.List;
20-
import java.util.Set;
21-
223
import org.apache.commons.io.FileUtils;
234
import org.apache.commons.io.IOUtils;
24-
import org.eclipse.jgit.api.CheckoutCommand;
5+
import org.eclipse.jgit.api.*;
256
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
26-
import org.eclipse.jgit.api.Git;
27-
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
28-
import org.eclipse.jgit.api.LogCommand;
29-
import org.eclipse.jgit.api.MergeResult;
30-
import org.eclipse.jgit.api.PushCommand;
317
import org.eclipse.jgit.api.ResetCommand.ResetType;
328
import org.eclipse.jgit.api.errors.GitAPIException;
339
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
3410
import org.eclipse.jgit.diff.DiffEntry;
3511
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
3612
import org.eclipse.jgit.diff.DiffEntry.Side;
3713
import org.eclipse.jgit.diff.DiffFormatter;
38-
import org.eclipse.jgit.lib.Constants;
39-
import org.eclipse.jgit.lib.ObjectId;
40-
import org.eclipse.jgit.lib.ObjectReader;
41-
import org.eclipse.jgit.lib.Ref;
42-
import org.eclipse.jgit.lib.Repository;
43-
import org.eclipse.jgit.revwalk.RevCommit;
44-
import org.eclipse.jgit.revwalk.RevObject;
45-
import org.eclipse.jgit.revwalk.RevSort;
46-
import org.eclipse.jgit.revwalk.RevTag;
47-
import org.eclipse.jgit.revwalk.RevWalk;
14+
import org.eclipse.jgit.lib.*;
15+
import org.eclipse.jgit.revwalk.*;
4816
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4917
import org.eclipse.jgit.transport.CredentialsProvider;
5018
import org.eclipse.jgit.transport.RefSpec;
5119
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
5220
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
53-
import org.scm4j.vcs.api.IVCS;
54-
import org.scm4j.vcs.api.VCSChangeType;
55-
import org.scm4j.vcs.api.VCSCommit;
56-
import org.scm4j.vcs.api.VCSDiffEntry;
57-
import org.scm4j.vcs.api.VCSMergeResult;
58-
import org.scm4j.vcs.api.VCSTag;
59-
import org.scm4j.vcs.api.WalkDirection;
21+
import org.eclipse.jgit.treewalk.TreeWalk;
22+
import org.eclipse.jgit.treewalk.filter.PathFilter;
23+
import org.scm4j.vcs.api.*;
6024
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
6125
import org.scm4j.vcs.api.exceptions.EVCSException;
6226
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
@@ -65,6 +29,12 @@
6529
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
6630
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
6731

32+
import java.io.*;
33+
import java.net.*;
34+
import java.net.Proxy.Type;
35+
import java.nio.charset.StandardCharsets;
36+
import java.util.*;
37+
6838
public class GitVCS implements IVCS {
6939

7040
public static final String GIT_VCS_TYPE_STRING = "git";
@@ -319,25 +289,30 @@ public String getFileContent(String branchName, String fileRelativePath, String
319289
Git git = getLocalGit(wc);
320290
Repository gitRepo = git.getRepository()) {
321291

322-
checkout(git, gitRepo, branchName, revision);
323-
File file = new File(wc.getFolder(), fileRelativePath);
324-
if (!file.exists()) {
292+
git
293+
.fetch()
294+
.setRefSpecs(new RefSpec("+refs/heads/*:refs/heads/*"))
295+
//.setRemoveDeletedRefs(true)
296+
.setCredentialsProvider(credentials)
297+
.call();
298+
git.pull().call(); //TODO: add test when we receive correct file version if we change it from another LWC
299+
RevWalk revWalk = new RevWalk(gitRepo);
300+
TreeWalk treeWalk = new TreeWalk(gitRepo);
301+
ObjectId revisionCommitId = gitRepo.resolve(revision == null ? "refs/heads/" + getRealBranchName(branchName) : revision);
302+
RevCommit commit = revWalk.parseCommit(revisionCommitId);
303+
RevTree tree = commit.getTree();
304+
treeWalk.addTree(tree);
305+
treeWalk.setRecursive(true);
306+
treeWalk.setFilter(PathFilter.create(fileRelativePath));
307+
if (!treeWalk.next()) {
325308
throw new EVCSFileNotFound(String.format("File %s is not found", fileRelativePath));
326309
}
327-
String res = IOUtils.toString(file.toURI(), StandardCharsets.UTF_8);
328-
329-
if (revision != null) {
330-
// leaving Detached HEAD state
331-
String bn = getRealBranchName(branchName);
332-
git
333-
.checkout()
334-
.setStartPoint("origin/" + bn)
335-
.setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null)
336-
.setUpstreamMode(SetupUpstreamMode.TRACK)
337-
.setName(bn)
338-
.call();
339-
}
340-
return res;
310+
ObjectId objectId = treeWalk.getObjectId(0);
311+
312+
ObjectLoader loader = gitRepo.open(objectId);
313+
InputStream in = loader.openStream();
314+
return IOUtils.toString(in, StandardCharsets.UTF_8);
315+
341316
} catch(EVCSFileNotFound e) {
342317
throw e;
343318
} catch (GitAPIException e) {
@@ -469,15 +444,9 @@ public Set<String> getBranches(String path) {
469444
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
470445
Git git = getLocalGit(wc);
471446
Repository gitRepo = git.getRepository()) {
472-
473-
git
474-
.pull()
475-
.call();
476447

477-
List<Ref> refs = git
478-
.branchList()
479-
.setListMode(ListMode.REMOTE)
480-
.call();
448+
git.pull().call(); //TODO: add test when we receive correct branches list if we change it from another LWC
449+
Collection<Ref> refs = gitRepo.getRefDatabase().getRefs(REFS_REMOTES_ORIGIN).values();
481450
Set<String> res = new HashSet<>();
482451
String bn;
483452
for (Ref ref : refs) {
@@ -728,11 +697,9 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
728697
Git git = getLocalGit(wc);
729698
Repository gitRepo = git.getRepository();
730699
RevWalk rw = new RevWalk(gitRepo)) {
731-
732-
git
733-
.pull()
734-
.call();
735-
700+
701+
updateLocalTags(git);
702+
736703
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
737704

738705
Ref ref = git
@@ -758,14 +725,28 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
758725
}
759726
}
760727

728+
private void updateLocalTags(Git git) throws Exception {
729+
// need to remove tags from local repo which are removed in origin
730+
git
731+
.fetch()
732+
.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"))
733+
.setRemoveDeletedRefs(true)
734+
.setCredentialsProvider(credentials)
735+
.call();
736+
git
737+
.pull()
738+
.call();
739+
}
740+
761741
@Override
762742
public List<VCSTag> getTags() {
763743
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
764744
Git git = getLocalGit(wc);
765745
Repository gitRepo = git.getRepository();
766746
RevWalk rw = new RevWalk(gitRepo)) {
767-
768-
List<Ref> tagRefs = getTagRefs(git);
747+
748+
updateLocalTags(git);
749+
Collection<Ref> tagRefs = gitRepo.getTags().values();
769750
List<VCSTag> res = new ArrayList<>();
770751
RevCommit revCommit;
771752
for (Ref ref : tagRefs) {
@@ -789,36 +770,35 @@ public List<VCSTag> getTags() {
789770
}
790771
}
791772

792-
private List<Ref> getTagRefs(Git git) throws Exception {
793-
// need to remove tags from local repo which are removed in origin
794-
git
795-
.fetch()
796-
.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"))
797-
.setRemoveDeletedRefs(true)
798-
.setCredentialsProvider(credentials)
799-
.call();
800-
return git
801-
.tagList()
802-
.call();
803-
}
773+
// private List<Ref> getTagRefs(Git git) throws Exception {
774+
// // need to remove tags from local repo which are removed in origin
775+
// git
776+
// .fetch()
777+
// .setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"))
778+
// .setRemoveDeletedRefs(true)
779+
// .setCredentialsProvider(credentials)
780+
// .call();
781+
// return git
782+
// .tagList()
783+
// .call();
784+
// }
804785

805786
@Override
806787
public void removeTag(String tagName) {
807788
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
808789
Git git = getLocalGit(wc);
809790
Repository gitRepo = git.getRepository();
810791
RevWalk rw = new RevWalk(gitRepo)) {
811-
812-
git.pull().call();
792+
793+
updateLocalTags(git);
813794

814795
git
815796
.tagDelete()
816797
.setTags(tagName)
817798
.call();
818799

819800
push(git, new RefSpec(":refs/tags/" + tagName));
820-
821-
801+
822802
} catch (GitAPIException e) {
823803
throw new EVCSException(e);
824804
} catch (Exception e) {
@@ -846,10 +826,11 @@ public List<VCSTag> getTagsOnRevision(String revision) {
846826
Git git = getLocalGit(wc);
847827
Repository gitRepo = git.getRepository();
848828
RevWalk rw = new RevWalk(gitRepo)) {
849-
829+
830+
updateLocalTags(git);
831+
850832
List<VCSTag> res = new ArrayList<>();
851-
852-
List<Ref> tagRefs = getTagRefs(git);
833+
Collection<Ref> tagRefs = gitRepo.getTags().values();
853834
RevCommit revCommit;
854835
for (Ref ref : tagRefs) {
855836
ObjectId relatedCommitObjectId = ref.getPeeledObjectId() == null ? ref.getObjectId() : ref.getPeeledObjectId();

0 commit comments

Comments
 (0)