Skip to content

Commit e0f9cd2

Browse files
committed
createUnannotatedTag() is moved to GitVCS and exposed as an addition to IVCS. Documentation updated
1 parent efb4cfd commit e0f9cd2

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Features:
5252
```
5353
- Use methods of `IVCS` interface. See [scm4j-vcs-api](https://github.com/scm4j/scm4j-vcs-api) for details
5454
- Use `vcs.setProxy()` and `vcs.setCredentials()` if necessary
55+
- Use `VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag)` to create git unannontated tag with name `tagName` on `revisionToTag` commit of branch `branchName`. If `branchName` is null then master branch is used. If `revisionToTag` is null then head of branch `branchName` is used.
56+
-
5557

5658
# Implementation details
5759
- [JGit](https://eclipse.org/jgit/) is used as framework to work with Git repositories

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ protected Git getLocalGit(String folder) throws Exception {
104104
.setCredentialsProvider(credentials)
105105
.setNoCheckout(true)
106106
.setCloneAllBranches(true)
107-
//.setBranch(Constants.R_HEADS + Constants.MASTER)
108107
.call()
109108
.close();
110109
}
@@ -127,6 +126,36 @@ public VCSChangeType gitChangeTypeToVCSChangeType(ChangeType changeType) {
127126
return VCSChangeType.UNKNOWN;
128127
}
129128
}
129+
130+
public VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag) {
131+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
132+
Git git = getLocalGit(wc);
133+
Repository gitRepo = git.getRepository();
134+
RevWalk rw = new RevWalk(gitRepo)) {
135+
136+
git
137+
.pull()
138+
.call();
139+
140+
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
141+
142+
Ref ref = git
143+
.tag()
144+
.setAnnotated(false)
145+
.setName(tagName)
146+
.setObjectId(commitToTag)
147+
.call();
148+
149+
push(git, new RefSpec(ref.getName()));
150+
151+
return new VCSTag(tagName, null, null, revisionToTag == null ? getHeadCommit(branchName)
152+
: getVCSCommit(commitToTag));
153+
} catch (GitAPIException e) {
154+
throw new EVCSException(e);
155+
} catch (Exception e) {
156+
throw new RuntimeException(e);
157+
}
158+
}
130159

131160
@Override
132161
public void createBranch(String srcBranchName, String newBranchName, String commitMessage) {

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@
2323
import java.util.List;
2424

2525
import org.apache.commons.io.FileUtils;
26+
import org.apache.commons.lang3.ArrayUtils;
2627
import org.eclipse.jgit.api.Git;
2728
import org.eclipse.jgit.api.errors.GitAPIException;
2829
import org.eclipse.jgit.diff.DiffEntry;
29-
import org.eclipse.jgit.lib.ObjectId;
30-
import org.eclipse.jgit.lib.Ref;
3130
import org.eclipse.jgit.lib.Repository;
32-
import org.eclipse.jgit.revwalk.RevCommit;
33-
import org.eclipse.jgit.revwalk.RevWalk;
3431
import org.eclipse.jgit.transport.CredentialItem;
35-
import org.eclipse.jgit.transport.RefSpec;
3632
import org.junit.After;
3733
import org.junit.Test;
3834
import org.mockito.Mockito;
@@ -43,7 +39,6 @@
4339
import org.scm4j.vcs.api.VCSTag;
4440
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
4541
import org.scm4j.vcs.api.exceptions.EVCSException;
46-
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
4742
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
4843

4944
public class GitVCSTest extends VCSAbstractTest {
@@ -194,7 +189,7 @@ public void testExceptions() throws Exception {
194189
@SuppressWarnings("serial")
195190
GitAPIException eApi = new GitAPIException("test git exception") {};
196191
Exception eCommon = new Exception("test common exception");
197-
for (Method m : IVCS.class.getDeclaredMethods()) {
192+
for (Method m : ArrayUtils.addAll(IVCS.class.getDeclaredMethods(), GitVCS.class.getMethod("createUnannotatedTag", String.class, String.class, String.class))) {
198193
Object[] params = new Object[m.getParameterTypes().length];
199194
Integer i = 0;
200195
for (Class<?> clazz : m.getParameterTypes()) {
@@ -259,7 +254,7 @@ public void testGitVCSUtilsCreation() {
259254

260255
@Test
261256
public void testGetTagsUnannotated() throws Exception {
262-
createUnannotatedTag(null, TAG_NAME_1, null);
257+
git.createUnannotatedTag(null, TAG_NAME_1, null);
263258
List<VCSTag> tags = vcs.getTags();
264259
assertTrue(tags.size() == 1);
265260
VCSTag tag = tags.get(0);
@@ -269,35 +264,6 @@ public void testGetTagsUnannotated() throws Exception {
269264
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
270265
}
271266

272-
public VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag) throws Exception {
273-
try (IVCSLockedWorkingCopy wc = localVCSRepo.getVCSLockedWorkingCopy();
274-
Git localGit = git.getLocalGit(wc);
275-
Repository gitRepo = localGit.getRepository();
276-
RevWalk rw = new RevWalk(gitRepo)) {
277-
278-
git.checkout(localGit, gitRepo, branchName, null);
279-
280-
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
281-
282-
Ref ref = localGit
283-
.tag()
284-
.setAnnotated(false)
285-
.setName(tagName)
286-
.setObjectId(commitToTag)
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-
return new VCSTag(tagName, null, null, revisionToTag == null ? vcs.getHeadCommit(branchName)
297-
: git.getVCSCommit(commitToTag));
298-
}
299-
}
300-
301267
@Test
302268
public void testCheckoutExceptions() throws Exception {
303269
@SuppressWarnings("serial")
@@ -329,9 +295,9 @@ public void testGetTagsOnRevisionUnannotated() throws Exception {
329295
vcs.createBranch(null, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
330296
VCSCommit c3 = vcs.setFileContent(NEW_BRANCH, FILE1_NAME, LINE_3, FILE1_CONTENT_CHANGED_COMMIT_MESSAGE + " " + LINE_3);
331297

332-
VCSTag tag1 = createUnannotatedTag(null, TAG_NAME_1, c1.getRevision());
333-
VCSTag tag2 = createUnannotatedTag(null, TAG_NAME_2, c1.getRevision());
334-
VCSTag tag3 = createUnannotatedTag(NEW_BRANCH, TAG_NAME_3, c3.getRevision());
298+
VCSTag tag1 = git.createUnannotatedTag(null, TAG_NAME_1, c1.getRevision());
299+
VCSTag tag2 = git.createUnannotatedTag(null, TAG_NAME_2, c1.getRevision());
300+
VCSTag tag3 = git.createUnannotatedTag(NEW_BRANCH, TAG_NAME_3, c3.getRevision());
335301

336302
assertTrue(vcs.getTagsOnRevision(c1.getRevision()).containsAll(Arrays.asList(
337303
tag1, tag2)));

0 commit comments

Comments
 (0)