1 package org.apache.maven.scm.provider.hg.command.branch;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.scm.ScmBranchParameters;
27 import org.apache.maven.scm.ScmException;
28 import org.apache.maven.scm.ScmFile;
29 import org.apache.maven.scm.ScmFileSet;
30 import org.apache.maven.scm.ScmFileStatus;
31 import org.apache.maven.scm.ScmResult;
32 import org.apache.maven.scm.command.Command;
33 import org.apache.maven.scm.command.branch.AbstractBranchCommand;
34 import org.apache.maven.scm.command.branch.BranchScmResult;
35 import org.apache.maven.scm.provider.ScmProviderRepository;
36 import org.apache.maven.scm.provider.hg.HgUtils;
37 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
38 import org.apache.maven.scm.provider.hg.command.HgConsumer;
39 import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
40 import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
41 import org.codehaus.plexus.util.StringUtils;
42
43
44
45
46
47
48
49
50 public class HgBranchCommand
51 extends AbstractBranchCommand
52 implements Command
53 {
54
55 protected ScmResult executeBranchCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet,
56 String branch, String message )
57 throws ScmException
58 {
59 return executeBranchCommand( scmProviderRepository, fileSet, branch, new ScmBranchParameters( message ) );
60 }
61
62
63
64
65 protected ScmResult executeBranchCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet,
66 String branch, ScmBranchParameters scmBranchParameters )
67 throws ScmException
68 {
69
70 if ( StringUtils.isBlank( branch ) )
71 {
72 throw new ScmException( "branch must be specified" );
73 }
74
75 if ( !fileSet.getFileList().isEmpty() )
76 {
77 throw new ScmException( "This provider doesn't support branchging subsets of a directory" );
78 }
79
80 File workingDir = fileSet.getBasedir();
81
82
83 String[] branchCmd = new String[] { HgCommandConstants.BRANCH_CMD, branch };
84
85
86 HgConsumer branchConsumer = new HgConsumer( getLogger() )
87 {
88 public void doConsume( ScmFileStatus status, String trimmedLine )
89 {
90
91 }
92 };
93
94 ScmResult result = HgUtils.execute( branchConsumer, getLogger(), workingDir, branchCmd );
95 HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
96
97 if ( !result.isSuccess() )
98 {
99 throw new ScmException( "Error while executing command " + joinCmd( branchCmd ) );
100 }
101
102
103 String[] commitCmd =
104 new String[] { HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION,
105 scmBranchParameters.getMessage() };
106
107 result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, commitCmd );
108
109 if ( !result.isSuccess() )
110 {
111 throw new ScmException( "Error while executing command " + joinCmd( commitCmd ) );
112 }
113
114
115
116 if ( repository.isPushChanges() )
117 {
118 if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
119 {
120
121 String[] pushCmd = new String[] {
122 HgCommandConstants.PUSH_CMD,
123 HgCommandConstants.NEW_BRANCH_OPTION,
124 repository.getURI()
125 };
126
127 result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
128
129 if ( !result.isSuccess() )
130 {
131 throw new ScmException( "Error while executing command " + joinCmd( pushCmd ) );
132 }
133 }
134 }
135
136
137 String[] listCmd = new String[]{ HgCommandConstants.INVENTORY_CMD };
138 HgListConsumer listconsumer = new HgListConsumer( getLogger() );
139
140 result = HgUtils.execute( listconsumer, getLogger(), fileSet.getBasedir(), listCmd );
141
142 if ( !result.isSuccess() )
143 {
144 throw new ScmException( "Error while executing command " + joinCmd( listCmd ) );
145 }
146
147 List<ScmFile> files = listconsumer.getFiles();
148 List<ScmFile> fileList = new ArrayList<ScmFile>();
149 for ( ScmFile f : files )
150 {
151 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
152 }
153
154 return new BranchScmResult( fileList, result );
155 }
156
157 private String joinCmd( String[] cmd )
158 {
159 StringBuilder result = new StringBuilder();
160 for ( int i = 0; i < cmd.length; i++ )
161 {
162 String s = cmd[i];
163 result.append( s );
164 if ( i < cmd.length - 1 )
165 {
166 result.append( " " );
167 }
168 }
169 return result.toString();
170 }
171 }