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