View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.branch;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmFileStatus;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.command.branch.AbstractBranchCommand;
29  import org.apache.maven.scm.command.branch.BranchScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
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.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
36  import org.codehaus.plexus.util.cli.CommandLineUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  
39  /**
40   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
41   *
42   */
43  public class GitBranchCommand extends AbstractBranchCommand implements GitCommand {
44      /** {@inheritDoc} */
45      public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message)
46              throws ScmException {
47          if (branch == null || StringUtils.isEmpty(branch.trim())) {
48              throw new ScmException("branch name must be specified");
49          }
50  
51          if (!fileSet.getFileList().isEmpty()) {
52              throw new ScmException("This provider doesn't support branching subsets of a directory");
53          }
54  
55          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
56  
57          Commandline cl = createCommandLine(repository, fileSet.getBasedir(), branch);
58  
59          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
60          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
61          int exitCode;
62  
63          exitCode = GitCommandLineUtils.execute(cl, stdout, stderr);
64          if (exitCode != 0) {
65              return new BranchScmResult(cl.toString(), "The git-branch command failed.", stderr.getOutput(), false);
66          }
67  
68          if (repo.isPushChanges()) {
69              // and now push the branch to the upstream repository
70              Commandline clPush = createPushCommandLine(repository, fileSet, branch);
71  
72              exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
73              if (exitCode != 0) {
74                  return new BranchScmResult(
75                          clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
76              }
77          }
78  
79          // as last action we search for the branched files
80          GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED);
81  
82          Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
83  
84          exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
85          if (exitCode != 0) {
86              return new BranchScmResult(
87                      clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
88          }
89  
90          return new BranchScmResult(cl.toString(), listConsumer.getListedFiles());
91      }
92  
93      // ----------------------------------------------------------------------
94      //
95      // ----------------------------------------------------------------------
96  
97      public static Commandline createCommandLine(
98              GitScmProviderRepository repository, File workingDirectory, String branch) {
99          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "branch");
100 
101         cl.createArg().setValue(branch);
102 
103         return cl;
104     }
105 
106     public static Commandline createPushCommandLine(
107             GitScmProviderRepository repository, ScmFileSet fileSet, String branch) throws ScmException {
108         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push");
109 
110         cl.createArg().setValue(repository.getPushUrl());
111         cl.createArg().setValue("refs/heads/" + branch);
112 
113         return cl;
114     }
115 
116     /**
117      * Helper function to detect the current branch
118      */
119     public static String getCurrentBranch(GitScmProviderRepository repository, ScmFileSet fileSet) throws ScmException {
120         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "symbolic-ref");
121         cl.createArg().setValue("HEAD");
122 
123         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
124         GitCurrentBranchConsumer cbConsumer = new GitCurrentBranchConsumer();
125         int exitCode;
126 
127         exitCode = GitCommandLineUtils.execute(cl, cbConsumer, stderr);
128 
129         if (exitCode != 0) {
130             throw new ScmException("Detecting the current branch failed: " + stderr.getOutput());
131         }
132 
133         return cbConsumer.getBranchName();
134     }
135 }