1 package org.apache.maven.scm.provider.git.gitexe.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.ScmFileSet;
24 import org.apache.maven.scm.ScmFileStatus;
25 import org.apache.maven.scm.ScmResult;
26 import org.apache.maven.scm.command.branch.AbstractBranchCommand;
27 import org.apache.maven.scm.command.branch.BranchScmResult;
28 import org.apache.maven.scm.log.ScmLogger;
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.repository.GitScmProviderRepository;
32 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
33 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
34 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.codehaus.plexus.util.cli.CommandLineUtils;
37 import org.codehaus.plexus.util.cli.Commandline;
38
39 import java.io.File;
40
41
42
43
44
45 public class GitBranchCommand
46 extends AbstractBranchCommand
47 implements GitCommand
48 {
49
50 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message )
51 throws ScmException
52 {
53 if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
54 {
55 throw new ScmException( "branch name must be specified" );
56 }
57
58 if ( !fileSet.getFileList().isEmpty() )
59 {
60 throw new ScmException( "This provider doesn't support branching subsets of a directory" );
61 }
62
63 GitScmProviderRepository repository = (GitScmProviderRepository) repo;
64
65 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch );
66
67 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
68 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
69 int exitCode;
70
71 exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
72 if ( exitCode != 0 )
73 {
74 return new BranchScmResult( cl.toString(), "The git-branch command failed.", stderr.getOutput(), false );
75 }
76
77 if( repo.isPushChanges() )
78 {
79
80 Commandline clPush = createPushCommandLine( repository, fileSet, branch );
81
82 exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr, getLogger() );
83 if ( exitCode != 0 )
84 {
85 return new BranchScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(), false );
86 }
87 }
88
89
90 GitListConsumer listConsumer = new GitListConsumer( getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED );
91
92 Commandline clList = GitListCommand.createCommandLine( repository, fileSet.getBasedir() );
93
94 exitCode = GitCommandLineUtils.execute( clList, listConsumer, stderr, getLogger() );
95 if ( exitCode != 0 )
96 {
97 return new BranchScmResult( clList.toString(), "The git-ls-files command failed.", stderr.getOutput(),
98 false );
99 }
100
101 return new BranchScmResult( cl.toString(), listConsumer.getListedFiles() );
102 }
103
104
105
106
107
108 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
109 String branch )
110 {
111 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "branch" );
112
113 cl.createArg().setValue( branch );
114
115 return cl;
116 }
117
118 public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
119 String branch )
120 throws ScmException
121 {
122 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
123
124 cl.createArg().setValue( repository.getPushUrl() );
125 cl.createArg().setValue( "refs/heads/" + branch );
126
127 return cl;
128 }
129
130
131
132
133 public static String getCurrentBranch( ScmLogger logger, GitScmProviderRepository repository, ScmFileSet fileSet )
134 throws ScmException
135 {
136 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "symbolic-ref" );
137 cl.createArg().setValue( "HEAD" );
138
139 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
140 GitCurrentBranchConsumer cbConsumer = new GitCurrentBranchConsumer( logger );
141 int exitCode;
142
143 exitCode = GitCommandLineUtils.execute( cl, cbConsumer, stderr, logger );
144
145 if ( exitCode != 0 )
146 {
147 throw new ScmException( "Detecting the current branch failed: " + stderr.getOutput() );
148 }
149
150 return cbConsumer.getBranchName();
151 }
152 }