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