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.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.command.branch.AbstractBranchCommand;
28  import org.apache.maven.scm.command.branch.BranchScmResult;
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.gitexe.command.GitCommandLineUtils;
32  import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
33  import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
34  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
35  import org.codehaus.plexus.util.cli.CommandLineUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  /**
39   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
40   *
41   */
42  public class GitBranchCommand extends AbstractBranchCommand implements GitCommand {
43      /** {@inheritDoc} */
44      public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message)
45              throws ScmException {
46          if (branch == null || branch.trim().isEmpty()) {
47              throw new ScmException("branch name must be specified");
48          }
49  
50          if (!fileSet.getFileList().isEmpty()) {
51              throw new ScmException("This provider doesn't support branching subsets of a directory");
52          }
53  
54          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
55  
56          Commandline cl = createCommandLine(repository, fileSet.getBasedir(), branch);
57  
58          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
59          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
60          int exitCode;
61  
62          exitCode = GitCommandLineUtils.execute(cl, stdout, stderr);
63          if (exitCode != 0) {
64              return new BranchScmResult(cl.toString(), "The git-branch command failed.", stderr.getOutput(), false);
65          }
66  
67          if (repo.isPushChanges()) {
68              // and now push the branch to the upstream repository
69              Commandline clPush = createPushCommandLine(repository, fileSet, branch);
70  
71              exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
72              if (exitCode != 0) {
73                  return new BranchScmResult(
74                          clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
75              }
76          }
77  
78          // as last action we search for the branched files
79          GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED);
80  
81          Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
82  
83          exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
84          if (exitCode != 0) {
85              return new BranchScmResult(
86                      clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
87          }
88  
89          return new BranchScmResult(cl.toString(), listConsumer.getListedFiles());
90      }
91  
92      // ----------------------------------------------------------------------
93      //
94      // ----------------------------------------------------------------------
95  
96      public static Commandline createCommandLine(
97              GitScmProviderRepository repository, File workingDirectory, String branch) {
98          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "branch");
99  
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 }