1 package org.apache.maven.scm.provider.git.gitexe.command.untag;
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
24 import org.apache.maven.scm.ScmException;
25 import org.apache.maven.scm.ScmFileSet;
26 import org.apache.maven.scm.ScmResult;
27 import org.apache.maven.scm.ScmUntagParameters;
28 import org.apache.maven.scm.command.untag.AbstractUntagCommand;
29 import org.apache.maven.scm.command.untag.UntagScmResult;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.git.command.GitCommand;
32 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
33 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.cli.CommandLineUtils;
36 import org.codehaus.plexus.util.cli.Commandline;
37
38
39 public class GitUntagCommand
40 extends AbstractUntagCommand
41 implements GitCommand
42 {
43
44
45 public ScmResult executeUntagCommand( ScmProviderRepository repo, ScmFileSet fileSet,
46 ScmUntagParameters scmUntagParameters )
47 throws ScmException
48 {
49 String tag = scmUntagParameters.getTag();
50 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
51 {
52 throw new ScmException( "tag name must be specified" );
53 }
54
55 GitScmProviderRepository repository = (GitScmProviderRepository) repo;
56
57 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
58 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59
60 int exitCode;
61
62 Commandline clTag = createCommandLine( repository, fileSet.getBasedir(), tag );
63
64 exitCode = GitCommandLineUtils.execute( clTag, stdout, stderr );
65 if ( exitCode != 0 )
66 {
67 return new UntagScmResult( clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false );
68 }
69
70 if ( repo.isPushChanges() )
71 {
72
73 Commandline clPush = createPushCommandLine( repository, fileSet, tag );
74
75 exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr );
76 if ( exitCode != 0 )
77 {
78 return new UntagScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(),
79 false );
80 }
81 }
82
83 return new UntagScmResult( clTag.toString() );
84 }
85
86
87
88
89
90 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
91 String tag )
92 {
93 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "tag" );
94
95 cl.createArg().setValue( "-d" );
96 cl.createArg().setValue( tag );
97
98 return cl;
99 }
100
101 public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
102 String tag )
103 {
104 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
105
106 cl.createArg().setValue( "--delete" );
107 cl.createArg().setValue( repository.getPushUrl() );
108 cl.createArg().setValue( "refs/tags/" + tag );
109
110 return cl;
111 }
112
113 }