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.io.IOException;
22  import java.util.Collection;
23  import java.util.EnumSet;
24  import java.util.Optional;
25  import java.util.function.BiFunction;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmUntagParameters;
31  import org.apache.maven.scm.command.untag.AbstractUntagCommand;
32  import org.apache.maven.scm.command.untag.UntagScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.git.command.GitCommand;
35  import org.apache.maven.scm.provider.git.jgit.command.CustomizableSshSessionFactoryCommand;
36  import org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback;
37  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
38  import org.apache.maven.scm.provider.git.jgit.command.PushException;
39  import org.apache.maven.scm.provider.git.jgit.command.ScmProviderAwareSshdSessionFactory;
40  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
41  import org.eclipse.jgit.api.Git;
42  import org.eclipse.jgit.api.TransportConfigCallback;
43  import org.eclipse.jgit.api.errors.GitAPIException;
44  import org.eclipse.jgit.lib.Constants;
45  import org.eclipse.jgit.transport.PushResult;
46  import org.eclipse.jgit.transport.RefSpec;
47  import org.eclipse.jgit.transport.RemoteRefUpdate;
48  import org.slf4j.Logger;
49  
50  /**
51   * {@inheritDoc}
52   */
53  public class JGitUntagCommand extends AbstractUntagCommand implements GitCommand, CustomizableSshSessionFactoryCommand {
54  
55      private BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory> sshSessionFactorySupplier;
56  
57      public JGitUntagCommand() {
58          sshSessionFactorySupplier = ScmProviderAwareSshdSessionFactory::new;
59      }
60  
61      @Override
62      public void setSshSessionFactorySupplier(
63              BiFunction<GitScmProviderRepository, Logger, ScmProviderAwareSshdSessionFactory>
64                      sshSessionFactorySupplier) {
65          this.sshSessionFactorySupplier = sshSessionFactorySupplier;
66      }
67  
68      @Override
69      protected ScmResult executeUntagCommand(
70              ScmProviderRepository repository, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters)
71              throws ScmException {
72          String tagName = scmUntagParameters.getTag();
73          if (tagName == null || tagName.trim().isEmpty()) {
74              throw new ScmException("tag name must be specified");
75          }
76          String escapedTagName = tagName.trim().replace(' ', '_');
77  
78          Git git = null;
79          try {
80              git = JGitUtils.openRepo(fileSet.getBasedir());
81  
82              // delete the tag
83              if (git.tagDelete().setTags(escapedTagName).call().isEmpty()) {
84                  return new UntagScmResult("JGit tagDelete", "Failed to delete tag", "", false);
85              }
86  
87              if (repository.isPushChanges()) {
88                  // From https://stackoverflow.com/q/11892766/696632
89                  RefSpec refSpec = new RefSpec().setSource(null).setDestination(Constants.R_TAGS + escapedTagName);
90  
91                  TransportConfigCallback transportConfigCallback = new JGitTransportConfigCallback(
92                          sshSessionFactorySupplier.apply((GitScmProviderRepository) repository, logger));
93  
94                  logger.info("push delete tag [" + escapedTagName + "] to remote...");
95  
96                  Iterable<PushResult> pushResultList = JGitUtils.push(
97                          git,
98                          (GitScmProviderRepository) repository,
99                          refSpec,
100                         EnumSet.of(
101                                 RemoteRefUpdate.Status.OK,
102                                 RemoteRefUpdate.Status.UP_TO_DATE,
103                                 RemoteRefUpdate.Status.NON_EXISTING),
104                         Optional.of(transportConfigCallback));
105                 if (logger.isInfoEnabled()) {
106                     for (PushResult pushResult : pushResultList) {
107                         Collection<RemoteRefUpdate> ru = pushResult.getRemoteUpdates();
108                         for (RemoteRefUpdate remoteRefUpdate : ru) {
109                             logger.info(remoteRefUpdate.getStatus() + " - " + remoteRefUpdate);
110                         }
111                     }
112                 }
113             }
114 
115             return new UntagScmResult("JGit tagDelete");
116         } catch (PushException e) {
117             logger.debug("Failed to push tag deletion", e);
118             return new UntagScmResult("JGit tagDelete", "Failed to push tag deletion: " + e.getMessage(), "", false);
119         } catch (IOException | GitAPIException e) {
120             throw new ScmException("JGit tagDelete failure!", e);
121         } finally {
122             JGitUtils.closeRepo(git);
123         }
124     }
125 }