1 package org.apache.maven.scm.provider.git.jgit.command.branch;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54 public class JGitBranchCommand
55 extends AbstractBranchCommand
56 implements GitCommand
57 {
58
59
60
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
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();
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
131
132
133
134
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 }