View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.jgit.command.untag;
20  
21  import java.util.Collection;
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.jgit.command.JGitUtils;
32  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
33  import org.eclipse.jgit.api.Git;
34  import org.eclipse.jgit.lib.Constants;
35  import org.eclipse.jgit.transport.PushResult;
36  import org.eclipse.jgit.transport.RefSpec;
37  import org.eclipse.jgit.transport.RemoteRefUpdate;
38  
39  /** {@inheritDoc} */
40  public class JGitUntagCommand extends AbstractUntagCommand implements GitCommand {
41  
42      @Override
43      protected ScmResult executeUntagCommand(
44              ScmProviderRepository repository, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters)
45              throws ScmException {
46          String tagName = scmUntagParameters.getTag();
47          if (tagName == null || tagName.trim().isEmpty()) {
48              throw new ScmException("tag name must be specified");
49          }
50          String escapedTagName = tagName.trim().replace(' ', '_');
51  
52          Git git = null;
53          try {
54              git = JGitUtils.openRepo(fileSet.getBasedir());
55  
56              // delete the tag
57              if (git.tagDelete().setTags(escapedTagName).call().isEmpty()) {
58                  return new UntagScmResult("JGit tagDelete", "Failed to delete tag", "", false);
59              }
60  
61              if (repository.isPushChanges()) {
62                  // From https://stackoverflow.com/q/11892766/696632
63                  RefSpec refSpec = new RefSpec().setSource(null).setDestination(Constants.R_TAGS + escapedTagName);
64  
65                  logger.info("push delete tag [" + escapedTagName + "] to remote...");
66  
67                  Iterable<PushResult> pushResultList =
68                          JGitUtils.push(git, (GitScmProviderRepository) repository, refSpec);
69                  if (logger.isInfoEnabled()) {
70                      for (PushResult pushResult : pushResultList) {
71                          Collection<RemoteRefUpdate> ru = pushResult.getRemoteUpdates();
72                          for (RemoteRefUpdate remoteRefUpdate : ru) {
73                              logger.info(remoteRefUpdate.getStatus() + " - " + remoteRefUpdate);
74                          }
75                      }
76                  }
77              }
78  
79              return new UntagScmResult("JGit tagDelete");
80          } catch (Exception e) {
81              throw new ScmException("JGit tagDelete failure!", e);
82          } finally {
83              JGitUtils.closeRepo(git);
84          }
85      }
86  }