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