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, 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              // and now push the branch to the upstream repository
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          // as last action we search for the branched files
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      * Helper function to detect the current branch 
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 }