1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git.jgit.command.tag;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFile;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmFileStatus;
29 import org.apache.maven.scm.ScmResult;
30 import org.apache.maven.scm.ScmTagParameters;
31 import org.apache.maven.scm.command.tag.AbstractTagCommand;
32 import org.apache.maven.scm.command.tag.TagScmResult;
33 import org.apache.maven.scm.provider.ScmProviderRepository;
34 import org.apache.maven.scm.provider.git.command.GitCommand;
35 import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
36 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
37 import org.eclipse.jgit.api.Git;
38 import org.eclipse.jgit.lib.Constants;
39 import org.eclipse.jgit.lib.Ref;
40 import org.eclipse.jgit.revwalk.RevCommit;
41 import org.eclipse.jgit.revwalk.RevWalk;
42 import org.eclipse.jgit.transport.RefSpec;
43 import org.eclipse.jgit.treewalk.TreeWalk;
44
45
46
47
48
49
50 public class JGitTagCommand extends AbstractTagCommand implements GitCommand {
51
52 public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message)
53 throws ScmException {
54 return executeTagCommand(repo, fileSet, tag, new ScmTagParameters(message));
55 }
56
57
58
59
60 public ScmResult executeTagCommand(
61 ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters)
62 throws ScmException {
63 if (tag == null || StringUtils.isEmpty(tag.trim())) {
64 throw new ScmException("tag name must be specified");
65 }
66
67 if (!fileSet.getFileList().isEmpty()) {
68 throw new ScmException("This provider doesn't support tagging subsets of a directory");
69 }
70
71 String escapedTagName = tag.trim().replace(' ', '_');
72
73 Git git = null;
74 try {
75 git = JGitUtils.openRepo(fileSet.getBasedir());
76
77
78 String tagMessage = scmTagParameters.getMessage();
79 Ref tagRef = git.tag()
80 .setName(escapedTagName)
81 .setMessage(tagMessage)
82 .setForceUpdate(false)
83 .call();
84
85 if (repo.isPushChanges()) {
86 logger.info("push tag [" + escapedTagName + "] to remote...");
87 JGitUtils.push(git, (GitScmProviderRepository) repo, new RefSpec(Constants.R_TAGS + escapedTagName));
88 }
89
90
91 RevWalk revWalk = new RevWalk(git.getRepository());
92 RevCommit commit = revWalk.parseCommit(tagRef.getObjectId());
93 revWalk.close();
94
95 final TreeWalk walk = new TreeWalk(git.getRepository());
96 walk.reset();
97 walk.setRecursive(true);
98 walk.addTree(commit.getTree());
99
100 List<ScmFile> taggedFiles = new ArrayList<>();
101 while (walk.next()) {
102 taggedFiles.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT));
103 }
104 walk.close();
105
106 return new TagScmResult("JGit tag", taggedFiles);
107 } catch (Exception e) {
108 throw new ScmException("JGit tag failure!", e);
109 } finally {
110 JGitUtils.closeRepo(git);
111 }
112 }
113 }