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.tag;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmTagParameters;
31  import org.apache.maven.scm.command.tag.AbstractTagCommand;
32  import org.apache.maven.scm.command.tag.TagScmResult;
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.JGitUtils;
36  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
37  import org.eclipse.jgit.api.Git;
38  import org.eclipse.jgit.lib.Constants;
39  import org.eclipse.jgit.lib.Ref;
40  import org.eclipse.jgit.revwalk.RevCommit;
41  import org.eclipse.jgit.revwalk.RevWalk;
42  import org.eclipse.jgit.transport.RefSpec;
43  import org.eclipse.jgit.treewalk.TreeWalk;
44  
45  /**
46   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
47   * @author Dominik Bartholdi (imod)
48   * @since 1.9
49   */
50  public class JGitTagCommand extends AbstractTagCommand implements GitCommand {
51  
52      public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message)
53              throws ScmException {
54          return executeTagCommand(repo, fileSet, tag, new ScmTagParameters(message));
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public ScmResult executeTagCommand(
61              ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters)
62              throws ScmException {
63          if (tag == null || StringUtils.isEmpty(tag.trim())) {
64              throw new ScmException("tag name must be specified");
65          }
66  
67          if (!fileSet.getFileList().isEmpty()) {
68              throw new ScmException("This provider doesn't support tagging subsets of a directory");
69          }
70  
71          String escapedTagName = tag.trim().replace(' ', '_');
72  
73          Git git = null;
74          try {
75              git = JGitUtils.openRepo(fileSet.getBasedir());
76  
77              // tag the revision
78              String tagMessage = scmTagParameters.getMessage();
79              Ref tagRef = git.tag()
80                      .setName(escapedTagName)
81                      .setMessage(tagMessage)
82                      .setForceUpdate(false)
83                      .call();
84  
85              if (repo.isPushChanges()) {
86                  logger.info("push tag [" + escapedTagName + "] to remote...");
87                  JGitUtils.push(git, (GitScmProviderRepository) repo, new RefSpec(Constants.R_TAGS + escapedTagName));
88              }
89  
90              // search for the tagged files
91              RevWalk revWalk = new RevWalk(git.getRepository());
92              RevCommit commit = revWalk.parseCommit(tagRef.getObjectId());
93              revWalk.close();
94  
95              final TreeWalk walk = new TreeWalk(git.getRepository());
96              walk.reset(); // drop the first empty tree, which we do not need here
97              walk.setRecursive(true);
98              walk.addTree(commit.getTree());
99  
100             List<ScmFile> taggedFiles = new ArrayList<>();
101             while (walk.next()) {
102                 taggedFiles.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT));
103             }
104             walk.close();
105 
106             return new TagScmResult("JGit tag", taggedFiles);
107         } catch (Exception e) {
108             throw new ScmException("JGit tag failure!", e);
109         } finally {
110             JGitUtils.closeRepo(git);
111         }
112     }
113 }