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  import java.util.Map;
23  
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      private final Map<String, String> environmentVariables;
45  
46      public GitBranchCommand(Map<String, String> environmentVariables) {
47          this.environmentVariables = environmentVariables;
48      }
49  
50      /** {@inheritDoc} */
51      public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message)
52              throws ScmException {
53          if (branch == null || branch.trim().isEmpty()) {
54              throw new ScmException("branch name must be specified");
55          }
56  
57          if (!fileSet.getFileList().isEmpty()) {
58              throw new ScmException("This provider doesn't support branching subsets of a directory");
59          }
60  
61          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
62  
63          Commandline cl = createCommandLine(repository, fileSet.getBasedir(), branch);
64  
65          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
66          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
67          int exitCode;
68  
69          exitCode = GitCommandLineUtils.execute(cl, stdout, stderr);
70          if (exitCode != 0) {
71              return new BranchScmResult(cl.toString(), "The git-branch command failed.", stderr.getOutput(), false);
72          }
73  
74          if (repo.isPushChanges()) {
75              // and now push the branch to the upstream repository
76              Commandline clPush = createPushCommandLine(repository, fileSet, branch);
77  
78              exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
79              if (exitCode != 0) {
80                  return new BranchScmResult(
81                          clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
82              }
83          }
84  
85          // as last action we search for the branched files
86          GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED);
87  
88          Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
89  
90          exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
91          if (exitCode != 0) {
92              return new BranchScmResult(
93                      clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
94          }
95  
96          return new BranchScmResult(cl.toString(), listConsumer.getListedFiles());
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102 
103     public static Commandline createCommandLine(
104             GitScmProviderRepository repository, File workingDirectory, String branch) {
105         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "branch");
106 
107         cl.createArg().setValue(branch);
108 
109         return cl;
110     }
111 
112     public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String branch) {
113         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
114                 fileSet.getBasedir(), "push", repository, environmentVariables);
115 
116         cl.createArg().setValue(repository.getPushUrl());
117         cl.createArg().setValue("refs/heads/" + branch);
118 
119         return cl;
120     }
121 
122     /**
123      * Helper function to detect the current branch
124      */
125     public static String getCurrentBranch(GitScmProviderRepository repository, ScmFileSet fileSet) throws ScmException {
126         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "symbolic-ref");
127         cl.createArg().setValue("HEAD");
128 
129         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
130         GitCurrentBranchConsumer cbConsumer = new GitCurrentBranchConsumer();
131         int exitCode;
132 
133         exitCode = GitCommandLineUtils.execute(cl, cbConsumer, stderr);
134 
135         if (exitCode != 0) {
136             throw new ScmException("Detecting the current branch failed: " + stderr.getOutput());
137         }
138 
139         return cbConsumer.getBranchName();
140     }
141 }