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
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
41
42
43 public class GitBranchCommand extends AbstractBranchCommand implements GitCommand {
44
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
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
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
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 }