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