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