1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
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
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 }