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, ScmTagParameters scmTagParameters )
61 throws ScmException
62 {
63 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
64 {
65 throw new ScmException( "tag name must be specified" );
66 }
67
68 if ( !fileSet.getFileList().isEmpty() )
69 {
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 {
79 FileUtils.fileWrite( messageFile.getAbsolutePath(), scmTagParameters.getMessage() );
80 }
81 catch ( IOException ex )
82 {
83 return new TagScmResult( null, "Error while making a temporary file for the commit message: "
84 + ex.getMessage(), null, false );
85 }
86
87 try
88 {
89 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
90 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
91
92 int exitCode;
93
94 Commandline clTag = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile );
95
96 exitCode = GitCommandLineUtils.execute( clTag, stdout, stderr, getLogger() );
97 if ( exitCode != 0 )
98 {
99 return new TagScmResult( clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false );
100 }
101
102 if( repo.isPushChanges() )
103 {
104
105 Commandline clPush = createPushCommandLine( repository, fileSet, tag );
106
107 exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr, getLogger() );
108 if ( exitCode != 0 )
109 {
110 return new TagScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(), false );
111 }
112 }
113
114
115 GitListConsumer listConsumer = new GitListConsumer( getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED );
116
117 Commandline clList = GitListCommand.createCommandLine( repository, fileSet.getBasedir() );
118
119 exitCode = GitCommandLineUtils.execute( clList, listConsumer, stderr, getLogger() );
120 if ( exitCode != 0 )
121 {
122 return new CheckOutScmResult( clList.toString(), "The git-ls-files command failed.",
123 stderr.getOutput(), false );
124 }
125
126 return new TagScmResult( clTag.toString(), listConsumer.getListedFiles() );
127 }
128 finally
129 {
130 try
131 {
132 FileUtils.forceDelete( messageFile );
133 }
134 catch ( IOException ex )
135 {
136
137 }
138 }
139
140 }
141
142
143
144
145
146 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
147 String tag, File messageFile )
148 {
149 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "tag" );
150
151 cl.createArg().setValue( "-F" );
152 cl.createArg().setValue( messageFile.getAbsolutePath() );
153
154
155 cl.createArg().setValue( tag );
156
157 return cl;
158 }
159
160 public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet, String tag )
161 throws ScmException
162 {
163 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
164
165 cl.createArg().setValue( repository.getPushUrl() );
166 cl.createArg().setValue( "refs/tags/" + tag );
167
168 return cl;
169 }
170
171 }