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.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.jgit.command.JGitUtils;
33  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34  import org.eclipse.jgit.api.Git;
35  import org.eclipse.jgit.lib.Constants;
36  import org.eclipse.jgit.transport.PushResult;
37  import org.eclipse.jgit.transport.RefSpec;
38  import org.eclipse.jgit.transport.RemoteRefUpdate;
39  
40  /** {@inheritDoc} */
41  public class JGitUntagCommand extends AbstractUntagCommand implements GitCommand {
42  
43      @Override
44      protected ScmResult executeUntagCommand(
45              ScmProviderRepository repository, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters)
46              throws ScmException {
47          String tagName = scmUntagParameters.getTag();
48          if (tagName == null || StringUtils.isEmpty(tagName.trim())) {
49              throw new ScmException("tag name must be specified");
50          }
51          String escapedTagName = tagName.trim().replace(' ', '_');
52  
53          Git git = null;
54          try {
55              git = JGitUtils.openRepo(fileSet.getBasedir());
56  
57              // delete the tag
58              if (git.tagDelete().setTags(escapedTagName).call().isEmpty()) {
59                  return new UntagScmResult("JGit tagDelete", "Failed to delete tag", "", false);
60              }
61  
62              if (repository.isPushChanges()) {
63                  // From https://stackoverflow.com/q/11892766/696632
64                  RefSpec refSpec = new RefSpec().setSource(null).setDestination(Constants.R_TAGS + escapedTagName);
65  
66                  logger.info("push delete tag [" + escapedTagName + "] to remote...");
67  
68                  Iterable<PushResult> pushResultList =
69                          JGitUtils.push(git, (GitScmProviderRepository) repository, refSpec);
70                  if (logger.isInfoEnabled()) {
71                      for (PushResult pushResult : pushResultList) {
72                          Collection<RemoteRefUpdate> ru = pushResult.getRemoteUpdates();
73                          for (RemoteRefUpdate remoteRefUpdate : ru) {
74                              logger.info(remoteRefUpdate.getStatus() + " - " + remoteRefUpdate);
75                          }
76                      }
77                  }
78              }
79  
80              return new UntagScmResult("JGit tagDelete");
81          } catch (Exception e) {
82              throw new ScmException("JGit tagDelete failure!", e);
83          } finally {
84              JGitUtils.closeRepo(git);
85          }
86      }
87  }