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