View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.branch;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.command.branch.AbstractBranchCommand;
28  import org.apache.maven.scm.command.branch.BranchScmResult;
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.codehaus.plexus.util.StringUtils;
34  import org.eclipse.jgit.api.Git;
35  import org.eclipse.jgit.api.errors.GitAPIException;
36  import org.eclipse.jgit.lib.Constants;
37  import org.eclipse.jgit.lib.Ref;
38  import org.eclipse.jgit.lib.Repository;
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  import java.util.ArrayList;
45  import java.util.HashSet;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Set;
49  
50  /**
51   * @author Dominik Bartholdi (imod)
52   * @since 1.9
53   */
54  public class JGitBranchCommand
55      extends AbstractBranchCommand
56      implements GitCommand
57  {
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      protected ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
64                                                String message )
65          throws ScmException
66      {
67          if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
68          {
69              throw new ScmException( "branch name must be specified" );
70          }
71  
72          if ( !fileSet.getFileList().isEmpty() )
73          {
74              throw new ScmException( "This provider doesn't support branching subsets of a directory" );
75          }
76  
77          Git git = null;
78          try
79          {
80              git = Git.open( fileSet.getBasedir() );
81              Ref branchResult = git.branchCreate().setName( branch ).call();
82              getLogger().info( "created [" + branchResult.getName() + "]" );
83  
84              if ( getLogger().isDebugEnabled() )
85              {
86                  for ( String branchName : getShortLocalBranchNames( git ) )
87                  {
88                      getLogger().debug( "local branch available: " + branchName );
89                  }
90              }
91  
92              if ( repo.isPushChanges() )
93              {
94                  getLogger().info( "push branch [" + branch + "] to remote..." );
95                  JGitUtils.push( getLogger(), git, (GitScmProviderRepository) repo, new RefSpec( Constants.R_HEADS
96                      + branch ) );
97              }
98  
99              // search for the tagged files
100             final RevWalk revWalk = new RevWalk( git.getRepository() );
101             RevCommit commit = revWalk.parseCommit( branchResult.getObjectId() );
102             revWalk.release();
103 
104             final TreeWalk walk = new TreeWalk( git.getRepository() );
105             walk.reset(); // drop the first empty tree, which we do not need here
106             walk.setRecursive( true );
107             walk.addTree( commit.getTree() );
108 
109             List<ScmFile> files = new ArrayList<ScmFile>();
110             while ( walk.next() )
111             {
112                 files.add( new ScmFile( walk.getPathString(), ScmFileStatus.CHECKED_OUT ) );
113             }
114             walk.release();
115 
116             return new BranchScmResult( "JGit branch", files );
117 
118         }
119         catch ( Exception e )
120         {
121             throw new ScmException( "JGit branch failed!", e );
122         }
123         finally
124         {
125             JGitUtils.closeRepo( git );
126         }
127     }
128 
129     /**
130      * gets a set of names of the available branches in the given repo
131      * 
132      * @param git the repo to list the branches for
133      * @return set of short branch names
134      * @throws GitAPIException
135      */
136     public static Set<String> getShortLocalBranchNames( Git git )
137         throws GitAPIException
138     {
139         Set<String> branches = new HashSet<String>();
140         Iterator<Ref> iter = git.branchList().call().iterator();
141         while ( iter.hasNext() )
142         {
143             branches.add( Repository.shortenRefName( iter.next().getName() ) );
144         }
145         return branches;
146     }
147 }