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