|
1 | 1 | package org.scm4j.vcs;
|
2 | 2 |
|
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.net.Proxy; |
| 9 | +import java.net.Proxy.Type; |
| 10 | +import java.net.ProxySelector; |
| 11 | +import java.net.SocketAddress; |
| 12 | +import java.net.URI; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 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; |
| 19 | + |
3 | 20 | import org.apache.commons.io.FileUtils;
|
4 | 21 | import org.apache.commons.io.IOUtils;
|
5 | 22 | import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
|
|
13 | 30 | import org.eclipse.jgit.diff.DiffEntry.ChangeType;
|
14 | 31 | import org.eclipse.jgit.diff.DiffEntry.Side;
|
15 | 32 | import org.eclipse.jgit.diff.DiffFormatter;
|
16 |
| -import org.eclipse.jgit.lib.*; |
| 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; |
17 | 38 | import org.eclipse.jgit.revwalk.RevCommit;
|
18 | 39 | import org.eclipse.jgit.revwalk.RevSort;
|
| 40 | +import org.eclipse.jgit.revwalk.RevTag; |
19 | 41 | import org.eclipse.jgit.revwalk.RevWalk;
|
20 | 42 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
21 | 43 | import org.eclipse.jgit.transport.CredentialsProvider;
|
22 | 44 | import org.eclipse.jgit.transport.RefSpec;
|
23 | 45 | import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
24 | 46 | import org.eclipse.jgit.treewalk.CanonicalTreeParser;
|
25 |
| -import org.scm4j.vcs.api.*; |
| 47 | +import org.scm4j.vcs.api.IVCS; |
| 48 | +import org.scm4j.vcs.api.VCSChangeType; |
| 49 | +import org.scm4j.vcs.api.VCSCommit; |
| 50 | +import org.scm4j.vcs.api.VCSDiffEntry; |
| 51 | +import org.scm4j.vcs.api.VCSMergeResult; |
| 52 | +import org.scm4j.vcs.api.VCSTag; |
| 53 | +import org.scm4j.vcs.api.WalkDirection; |
26 | 54 | import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
|
27 | 55 | import org.scm4j.vcs.api.exceptions.EVCSException;
|
28 | 56 | import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
|
| 57 | +import org.scm4j.vcs.api.exceptions.EVCSTagExists; |
29 | 58 | import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
|
30 | 59 | import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
|
31 | 60 | import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
|
32 | 61 |
|
33 |
| -import java.io.ByteArrayOutputStream; |
34 |
| -import java.io.File; |
35 |
| -import java.io.FileWriter; |
36 |
| -import java.io.IOException; |
37 |
| -import java.net.*; |
38 |
| -import java.net.Proxy.Type; |
39 |
| -import java.nio.charset.StandardCharsets; |
40 |
| -import java.util.*; |
41 |
| - |
42 | 62 | public class GitVCS implements IVCS {
|
43 | 63 |
|
44 | 64 | private static final String MASTER_BRANCH_NAME = "master";
|
@@ -685,4 +705,80 @@ public String toString() {
|
685 | 705 | public Boolean fileExists(String branchName, String filePath) {
|
686 | 706 | return getFileFromRepo(branchName, filePath).exists();
|
687 | 707 | }
|
| 708 | + |
| 709 | + @Override |
| 710 | + public VCSTag createTag(String branchName, String tagName, String tagMessage) throws EVCSTagExists { |
| 711 | + try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy(); |
| 712 | + Git git = getLocalGit(wc); |
| 713 | + Repository gitRepo = git.getRepository(); |
| 714 | + RevWalk rw = new RevWalk(gitRepo)) { |
| 715 | + |
| 716 | + String bn = getRealBranchName(branchName); |
| 717 | + |
| 718 | + git |
| 719 | + .checkout() |
| 720 | + .setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null) |
| 721 | + .setName(bn) |
| 722 | + .call(); |
| 723 | + |
| 724 | + git |
| 725 | + .pull() |
| 726 | + .setCredentialsProvider(credentials) |
| 727 | + .call(); |
| 728 | + |
| 729 | + Ref ref = git |
| 730 | + .tag() |
| 731 | + .setAnnotated(true) |
| 732 | + .setMessage(tagMessage) |
| 733 | + .setName(tagName) |
| 734 | + .setObjectId(null) //rw.gparseCommit(ObjectId.fromString(commitIdToTag))) |
| 735 | + .call(); |
| 736 | + |
| 737 | + git |
| 738 | + .push() |
| 739 | + .setRefSpecs(new RefSpec(ref.getName())) |
| 740 | + .setCredentialsProvider(credentials) |
| 741 | + .call(); |
| 742 | + |
| 743 | + |
| 744 | + RevTag revTag = rw.parseTag(ref.getObjectId()); |
| 745 | + RevCommit revCommit = rw.parseCommit(ref.getObjectId()); |
| 746 | + VCSCommit relatedCommit = new VCSCommit(revCommit.getName(), revCommit.getFullMessage(), revCommit.getAuthorIdent().getName()); |
| 747 | + return new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit); |
| 748 | + } catch(RefAlreadyExistsException e) { |
| 749 | + throw new EVCSTagExists(e); |
| 750 | + } catch (GitAPIException e) { |
| 751 | + throw new EVCSException(e); |
| 752 | + } catch (Exception e) { |
| 753 | + throw new RuntimeException(e); |
| 754 | + } |
| 755 | + } |
| 756 | + |
| 757 | + @Override |
| 758 | + public List<VCSTag> getTags() { |
| 759 | + try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy(); |
| 760 | + Git git = getLocalGit(wc); |
| 761 | + Repository gitRepo = git.getRepository(); |
| 762 | + RevWalk rw = new RevWalk(gitRepo) ) { |
| 763 | + List<Ref> refs = git |
| 764 | + .tagList() |
| 765 | + .call(); |
| 766 | + |
| 767 | + RevTag revTag; |
| 768 | + RevCommit revCommit; |
| 769 | + List<VCSTag> res = new ArrayList<>(); |
| 770 | + for (Ref ref : refs) { |
| 771 | + revTag = rw.parseTag(ref.getObjectId()); |
| 772 | + revCommit = rw.parseCommit(ref.getObjectId()); |
| 773 | + VCSCommit relatedCommit = new VCSCommit(revCommit.getName(), revCommit.getFullMessage(), revCommit.getAuthorIdent().getName()); |
| 774 | + VCSTag tag = new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit); |
| 775 | + res.add(tag); |
| 776 | + } |
| 777 | + return res; |
| 778 | + } catch (GitAPIException e) { |
| 779 | + throw new EVCSException(e); |
| 780 | + } catch (Exception e) { |
| 781 | + throw new RuntimeException(e); |
| 782 | + } |
| 783 | + } |
688 | 784 | }
|
0 commit comments