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.provider.ScmProviderRepository;
29 import org.apache.maven.scm.provider.git.command.GitCommand;
30 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
31 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
32 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
33 import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.cli.CommandLineUtils;
36 import org.codehaus.plexus.util.cli.Commandline;
37
38 import java.io.File;
39
40
41
42
43
44 public class GitBranchCommand
45 extends AbstractBranchCommand
46 implements GitCommand
47 {
48
49 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch,
50 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 );
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 );
83 if ( exitCode != 0 )
84 {
85 return new BranchScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(),
86 false );
87 }
88 }
89
90
91 GitListConsumer listConsumer = new GitListConsumer( fileSet.getBasedir(), ScmFileStatus.TAGGED );
92
93 Commandline clList = GitListCommand.createCommandLine( repository, fileSet.getBasedir() );
94
95 exitCode = GitCommandLineUtils.execute( clList, listConsumer, stderr );
96 if ( exitCode != 0 )
97 {
98 return new BranchScmResult( clList.toString(), "The git-ls-files command failed.", stderr.getOutput(),
99 false );
100 }
101
102 return new BranchScmResult( cl.toString(), listConsumer.getListedFiles() );
103 }
104
105
106
107
108
109 public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
110 String branch )
111 {
112 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "branch" );
113
114 cl.createArg().setValue( branch );
115
116 return cl;
117 }
118
119 public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
120 String branch )
121 throws ScmException
122 {
123 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
124
125 cl.createArg().setValue( repository.getPushUrl() );
126 cl.createArg().setValue( "refs/heads/" + branch );
127
128 return cl;
129 }
130
131
132
133
134 public static String getCurrentBranch( GitScmProviderRepository repository, ScmFileSet fileSet )
135 throws ScmException
136 {
137 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "symbolic-ref" );
138 cl.createArg().setValue( "HEAD" );
139
140 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
141 GitCurrentBranchConsumer cbConsumer = new GitCurrentBranchConsumer();
142 int exitCode;
143
144 exitCode = GitCommandLineUtils.execute( cl, cbConsumer, stderr );
145
146 if ( exitCode != 0 )
147 {
148 throw new ScmException( "Detecting the current branch failed: " + stderr.getOutput() );
149 }
150
151 return cbConsumer.getBranchName();
152 }
153 }