|
10 | 10 | import java.net.SocketAddress;
|
11 | 11 | import java.net.URI;
|
12 | 12 | import java.nio.charset.StandardCharsets;
|
| 13 | +import java.util.ArrayList; |
13 | 14 | import java.util.Arrays;
|
14 | 15 | import java.util.List;
|
15 | 16 |
|
|
19 | 20 | import org.eclipse.jgit.api.ResetCommand.ResetType;
|
20 | 21 | import org.eclipse.jgit.api.errors.GitAPIException;
|
21 | 22 | import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
|
| 23 | +import org.eclipse.jgit.diff.DiffEntry; |
| 24 | +import org.eclipse.jgit.diff.DiffEntry.Side; |
| 25 | +import org.eclipse.jgit.lib.ObjectReader; |
| 26 | +import org.eclipse.jgit.lib.Ref; |
22 | 27 | import org.eclipse.jgit.lib.Repository;
|
| 28 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 29 | +import org.eclipse.jgit.revwalk.RevTree; |
| 30 | +import org.eclipse.jgit.revwalk.RevWalk; |
23 | 31 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
24 | 32 | import org.eclipse.jgit.transport.CredentialsProvider;
|
25 | 33 | import org.eclipse.jgit.transport.RefSpec;
|
26 | 34 | import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
| 35 | +import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
| 36 | +import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
27 | 37 |
|
28 | 38 | import com.projectkaiser.scm.vcs.api.IVCS;
|
29 | 39 | import com.projectkaiser.scm.vcs.api.PKVCSMergeResult;
|
@@ -78,6 +88,8 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
|
78 | 88 | .setCredentialsProvider(credentials)
|
79 | 89 | .call();
|
80 | 90 | git.getRepository().close();
|
| 91 | + Thread.sleep(2000); // github has some latency on branch operations |
| 92 | + // so next request branches operation will return old branches list |
81 | 93 | }
|
82 | 94 | }
|
83 | 95 | } catch (RefAlreadyExistsException e) {
|
@@ -121,6 +133,8 @@ public void deleteBranch(String branchName, String commitMessage) {
|
121 | 133 | .setCredentialsProvider(credentials)
|
122 | 134 | .call();
|
123 | 135 | git.getRepository().close();
|
| 136 | + Thread.sleep(2000); // github has some latency on branch operations |
| 137 | + // so next request branches operation will return old branches list |
124 | 138 | }
|
125 | 139 | }
|
126 | 140 | } catch (GitAPIException e) {
|
@@ -346,4 +360,47 @@ public String getFileContent(String branchName, String filePath) {
|
346 | 360 | return getFileContent(branchName, filePath, StandardCharsets.UTF_8.name());
|
347 | 361 | }
|
348 | 362 |
|
| 363 | + @Override |
| 364 | + public List<String> getBranchesDiff(String srcBranchName, String destBranchName) { |
| 365 | + List<String> res = new ArrayList<>(); |
| 366 | + try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) { |
| 367 | + try (Git git = getLocalGit(wc)) { |
| 368 | + // the diff works on TreeIterators, we prepare two for the two branches |
| 369 | + AbstractTreeIterator oldTreeParser = prepareTreeParser(git.getRepository(), "refs/heads/" + srcBranchName); |
| 370 | + AbstractTreeIterator newTreeParser = prepareTreeParser(git.getRepository(), "refs/heads/" + destBranchName); |
| 371 | + |
| 372 | + // then the procelain diff-command returns a list of diff entries |
| 373 | + List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call(); |
| 374 | + for (DiffEntry entry : diff) { |
| 375 | + res.add(entry.getPath(Side.OLD)); |
| 376 | + } |
| 377 | + git.getRepository().close(); |
| 378 | + return res; |
| 379 | + } |
| 380 | + } catch (GitAPIException e) { |
| 381 | + throw new EVCSException(e); |
| 382 | + } catch (Exception e) { |
| 383 | + throw new RuntimeException(e); |
| 384 | + } |
| 385 | + |
| 386 | + } |
| 387 | + |
| 388 | + private AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws Exception { |
| 389 | + // from the commit we can build the tree which allows us to construct the TreeParser |
| 390 | + Ref head = repository.exactRef(ref); |
| 391 | + try (RevWalk walk = new RevWalk(repository)) { |
| 392 | + RevCommit commit = walk.parseCommit(head.getObjectId()); |
| 393 | + RevTree tree = walk.parseTree(commit.getTree().getId()); |
| 394 | + |
| 395 | + CanonicalTreeParser oldTreeParser = new CanonicalTreeParser(); |
| 396 | + try (ObjectReader oldReader = repository.newObjectReader()) { |
| 397 | + oldTreeParser.reset(oldReader, tree.getId()); |
| 398 | + } |
| 399 | + |
| 400 | + walk.dispose(); |
| 401 | + |
| 402 | + return oldTreeParser; |
| 403 | + } |
| 404 | + } |
| 405 | + |
349 | 406 | }
|
0 commit comments