1 package org.apache.maven.scm.provider.hg.command.tag;
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.ScmException;
27 import org.apache.maven.scm.ScmFile;
28 import org.apache.maven.scm.ScmFileSet;
29 import org.apache.maven.scm.ScmFileStatus;
30 import org.apache.maven.scm.ScmResult;
31 import org.apache.maven.scm.ScmTagParameters;
32 import org.apache.maven.scm.command.Command;
33 import org.apache.maven.scm.command.tag.AbstractTagCommand;
34 import org.apache.maven.scm.command.tag.TagScmResult;
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 HgTagCommand
51 extends AbstractTagCommand
52 implements Command
53 {
54
55 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
56 String message )
57 throws ScmException
58 {
59 return executeTagCommand( scmProviderRepository, fileSet, tag, new ScmTagParameters( message ) );
60 }
61
62
63
64
65 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
66 ScmTagParameters scmTagParameters )
67 throws ScmException
68 {
69
70 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
71 {
72 throw new ScmException( "tag must be specified" );
73 }
74
75 if ( !fileSet.getFileList().isEmpty() )
76 {
77 throw new ScmException( "This provider doesn't support tagging subsets of a directory : "
78 + fileSet.getFileList() );
79 }
80
81 File workingDir = fileSet.getBasedir();
82
83
84 String[] tagCmd =
85 new String[]{ HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(),
86 tag };
87
88
89 StringBuilder cmd = joinCmd( tagCmd );
90 HgTagConsumer consumer = new HgTagConsumer( getLogger() );
91 ScmResult result = HgUtils.execute( consumer, getLogger(), workingDir, tagCmd );
92 HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
93 if ( result.isSuccess() )
94 {
95
96
97
98 if ( repository.isPushChanges() )
99 {
100 if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
101 {
102 String branchName = HgUtils.getCurrentBranchName( getLogger(), workingDir );
103 boolean differentOutgoingBranch =
104 HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName );
105
106 String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD,
107 differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
108 repository.getURI() };
109
110 result =
111 HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
112 }
113 }
114 }
115 else
116 {
117 throw new ScmException( "Error while executing command " + cmd.toString() );
118 }
119
120
121 String[] listCmd = new String[]{ HgCommandConstants.INVENTORY_CMD };
122 HgListConsumer listconsumer = new HgListConsumer( getLogger() );
123 result = HgUtils.execute( listconsumer, getLogger(), fileSet.getBasedir(), listCmd );
124 if ( result.isSuccess() )
125 {
126 List<ScmFile> files = listconsumer.getFiles();
127 List<ScmFile> fileList = new ArrayList<ScmFile>();
128 for ( ScmFile f : files )
129 {
130 if ( !f.getPath().endsWith( ".hgtags" ) )
131 {
132 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
133 }
134 }
135
136 return new TagScmResult( fileList, result );
137 }
138 else
139 {
140 throw new ScmException( "Error while executing command " + cmd.toString() );
141 }
142 }
143
144 private StringBuilder joinCmd( String[] cmd )
145 {
146 StringBuilder result = new StringBuilder();
147 for ( int i = 0; i < cmd.length; i++ )
148 {
149 String s = cmd[i];
150 result.append( s );
151 if ( i < cmd.length - 1 )
152 {
153 result.append( " " );
154 }
155 }
156 return result;
157 }
158 }