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