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.gitexe.command.tag;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Map;
24
25 import org.apache.maven.scm.CommandParameters.SignOption;
26 import org.apache.maven.scm.ScmException;
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.checkout.CheckOutScmResult;
32 import org.apache.maven.scm.command.tag.AbstractTagCommand;
33 import org.apache.maven.scm.command.tag.TagScmResult;
34 import org.apache.maven.scm.provider.ScmProviderRepository;
35 import org.apache.maven.scm.provider.git.command.GitCommand;
36 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
37 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
38 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
39 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
40 import org.codehaus.plexus.util.FileUtils;
41 import org.codehaus.plexus.util.cli.CommandLineUtils;
42 import org.codehaus.plexus.util.cli.Commandline;
43
44
45
46
47 public class GitTagCommand extends AbstractTagCommand implements GitCommand {
48 private final Map<String, String> environmentVariables;
49
50 public GitTagCommand(Map<String, String> environmentVariables) {
51 this.environmentVariables = environmentVariables;
52 }
53
54 public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message)
55 throws ScmException {
56 return executeTagCommand(repo, fileSet, tag, new ScmTagParameters(message));
57 }
58
59
60
61
62 public ScmResult executeTagCommand(
63 ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters)
64 throws ScmException {
65 if (tag == null || tag.trim().isEmpty()) {
66 throw new ScmException("tag name must be specified");
67 }
68
69 if (!fileSet.getFileList().isEmpty()) {
70 throw new ScmException("This provider doesn't support tagging subsets of a directory");
71 }
72
73 GitScmProviderRepository repository = (GitScmProviderRepository) repo;
74
75 File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
76
77 try {
78 FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", scmTagParameters.getMessage());
79 } catch (IOException ex) {
80 return new TagScmResult(
81 null,
82 "Error while making a temporary file for the commit message: " + ex.getMessage(),
83 null,
84 false);
85 }
86
87 try {
88 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
89 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
90
91 int exitCode;
92
93 Commandline clTag = createCommandLine(
94 repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters.getSignOption());
95
96 exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr);
97 if (exitCode != 0) {
98 return new TagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false);
99 }
100
101 if (repo.isPushChanges()) {
102
103 Commandline clPush = createPushCommandLine(repository, fileSet, tag);
104
105 exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
106 if (exitCode != 0) {
107 return new TagScmResult(
108 clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
109 }
110 }
111
112
113 GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED);
114
115 Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
116
117 exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
118 if (exitCode != 0) {
119 return new CheckOutScmResult(
120 clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
121 }
122
123 return new TagScmResult(clTag.toString(), listConsumer.getListedFiles());
124 } finally {
125 try {
126 FileUtils.forceDelete(messageFile);
127 } catch (IOException ex) {
128
129 }
130 }
131 }
132
133
134
135
136
137 static Commandline createCommandLine(
138 GitScmProviderRepository repository,
139 File workingDirectory,
140 String tag,
141 File messageFile,
142 SignOption signOption) {
143 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag");
144
145 switch (signOption) {
146 case FORCE_SIGN:
147 cl.createArg().setValue("-s");
148 break;
149 case FORCE_NO_SIGN:
150 cl.createArg().setValue("--no-sign");
151 break;
152 default:
153
154 break;
155 }
156
157 cl.createArg().setValue("-F");
158 cl.createArg().setValue(messageFile.getAbsolutePath());
159
160
161 cl.createArg().setValue(tag);
162
163 return cl;
164 }
165
166 public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
167 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
168 fileSet.getBasedir(), "push", repository, environmentVariables);
169
170 cl.createArg().setValue(repository.getPushUrl());
171 cl.createArg().setValue("refs/tags/" + tag);
172
173 return cl;
174 }
175 }