001package org.apache.maven.scm.provider.git.gitexe.command.branch;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmFileStatus;
025import org.apache.maven.scm.ScmResult;
026import org.apache.maven.scm.command.branch.AbstractBranchCommand;
027import org.apache.maven.scm.command.branch.BranchScmResult;
028import org.apache.maven.scm.log.ScmLogger;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.git.command.GitCommand;
031import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
032import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
033import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
034import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
035import org.codehaus.plexus.util.StringUtils;
036import org.codehaus.plexus.util.cli.CommandLineUtils;
037import org.codehaus.plexus.util.cli.Commandline;
038
039import java.io.File;
040
041/**
042 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
043 *
044 */
045public class GitBranchCommand
046    extends AbstractBranchCommand
047    implements GitCommand
048{
049    /** {@inheritDoc} */
050    public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message )
051        throws ScmException
052    {
053        if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
054        {
055            throw new ScmException( "branch name must be specified" );
056        }
057
058        if ( !fileSet.getFileList().isEmpty() )
059        {
060            throw new ScmException( "This provider doesn't support branching subsets of a directory" );
061        }
062
063        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
064
065        Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch );
066
067        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
068        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
069        int exitCode;
070
071        exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
072        if ( exitCode != 0 )
073        {
074            return new BranchScmResult( cl.toString(), "The git-branch command failed.", stderr.getOutput(), false );
075        }
076
077        if( repo.isPushChanges() ) 
078        {
079            // and now push the branch to the upstream repository
080            Commandline clPush = createPushCommandLine( repository, fileSet, branch );
081
082            exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr, getLogger() );
083            if ( exitCode != 0 )
084            {
085                return new BranchScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(), false );
086            }
087        }
088
089        // as last action we search for the branched files
090        GitListConsumer listConsumer = new GitListConsumer( getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED );
091
092        Commandline clList = GitListCommand.createCommandLine( repository, fileSet.getBasedir() );
093
094        exitCode = GitCommandLineUtils.execute( clList, listConsumer, stderr, getLogger() );
095        if ( exitCode != 0 )
096        {
097            return new BranchScmResult( clList.toString(), "The git-ls-files command failed.", stderr.getOutput(),
098                                        false );
099        }
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}