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