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,
051                                           String message )
052        throws ScmException
053    {
054        if ( branch == null || StringUtils.isEmpty( branch.trim() ) )
055        {
056            throw new ScmException( "branch name must be specified" );
057        }
058
059        if ( !fileSet.getFileList().isEmpty() )
060        {
061            throw new ScmException( "This provider doesn't support branching subsets of a directory" );
062        }
063
064        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
065
066        Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch );
067
068        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
069        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
070        int exitCode;
071
072        exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
073        if ( exitCode != 0 )
074        {
075            return new BranchScmResult( cl.toString(), "The git-branch command failed.", stderr.getOutput(), false );
076        }
077
078        if ( repo.isPushChanges() )
079        {
080            // and now push the branch to the upstream repository
081            Commandline clPush = createPushCommandLine( repository, fileSet, branch );
082
083            exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr, getLogger() );
084            if ( exitCode != 0 )
085            {
086                return new BranchScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(),
087                                            false );
088            }
089        }
090
091        // as last action we search for the branched files
092        GitListConsumer listConsumer = new GitListConsumer( getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED );
093
094        Commandline clList = GitListCommand.createCommandLine( repository, fileSet.getBasedir() );
095
096        exitCode = GitCommandLineUtils.execute( clList, listConsumer, stderr, getLogger() );
097        if ( exitCode != 0 )
098        {
099            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}