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