1 package org.apache.maven.scm.provider.accurev.command.checkout;
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.CommandParameters;
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.ScmResult;
31 import org.apache.maven.scm.ScmVersion;
32 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33 import org.apache.maven.scm.log.ScmLogger;
34 import org.apache.maven.scm.provider.ScmProviderRepository;
35 import org.apache.maven.scm.provider.accurev.AccuRev;
36 import org.apache.maven.scm.provider.accurev.AccuRevException;
37 import org.apache.maven.scm.provider.accurev.AccuRevInfo;
38 import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
39 import org.apache.maven.scm.provider.accurev.AccuRevVersion;
40 import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevExtractSourceCommand;
41
42
43
44
45 public class AccuRevCheckOutCommand
46 extends AbstractAccuRevExtractSourceCommand
47 {
48
49 public AccuRevCheckOutCommand( ScmLogger logger )
50 {
51 super( logger );
52 }
53
54 public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
55 CommandParameters parameters )
56 throws ScmException
57 {
58 return (CheckOutScmResult) execute( repository, fileSet, parameters );
59 }
60
61 @Override
62 protected List<File> extractSource( AccuRevScmProviderRepository repository, File basedir, AccuRevVersion version )
63 throws AccuRevException
64 {
65 AccuRev accuRev = repository.getAccuRev();
66
67 AccuRevInfo info = accuRev.info( basedir );
68
69 List<File> extractedFiles = new ArrayList<File>();
70
71 String basisStream = version.getBasisStream();
72 String transactionId = version.getTimeSpec();
73
74 boolean success = true;
75 if ( info.isWorkSpace() )
76 {
77
78 if ( !repository.isWorkSpaceTop( info ) )
79 {
80 throw new AccuRevException( String.format( "Can't checkout to %s, "
81 + "a subdirectory of existing workspace %s", basedir, info.getWorkSpace() ) );
82 }
83
84 if ( !basisStream.equals( info.getBasis() ) )
85 {
86
87 success = accuRev.chws( basedir, info.getWorkSpace(), basisStream );
88 }
89
90 if ( success )
91 {
92
93
94
95
96 List<File> poppedFiles = accuRev.pop( basedir, null );
97 if ( poppedFiles != null )
98 {
99 extractedFiles.addAll( poppedFiles );
100 }
101 else
102 {
103 success = false;
104 }
105 }
106
107 }
108 else
109 {
110
111
112
113
114 String workSpaceName = getWorkSpaceName( basedir, basisStream );
115
116 success = accuRev.mkws( basisStream, workSpaceName, basedir );
117
118
119
120
121
122 transactionId = "now";
123
124 if ( success )
125 {
126 getLogger().info( "Created workspace " + workSpaceName );
127 }
128 }
129
130 if ( success )
131 {
132 List<File> updatedFiles = accuRev.update( basedir, transactionId );
133 if ( updatedFiles != null )
134 {
135 extractedFiles.addAll( updatedFiles );
136 }
137 else
138 {
139 success = false;
140 }
141 }
142 return success ? extractedFiles : null;
143 }
144
145 @Override
146 protected ScmResult getScmResult( AccuRevScmProviderRepository repository, List<ScmFile> scmFiles,
147 ScmVersion version )
148 {
149 AccuRev accuRev = repository.getAccuRev();
150 if ( scmFiles != null )
151 {
152 return new CheckOutScmResult( accuRev.getCommandLines(), scmFiles, repository.getProjectPath() );
153 }
154 else
155 {
156 return new CheckOutScmResult( accuRev.getCommandLines(), "AccuRev Error", accuRev.getErrorOutput(), false );
157 }
158 }
159
160 public static String getWorkSpaceName( File basedir, String basisStream )
161 {
162 String workSpaceName;
163 String baseName = basedir.getName();
164 if ( baseName.contains( basisStream ) )
165 {
166 workSpaceName = baseName;
167 }
168 else if ( basisStream.contains( baseName ) )
169 {
170 workSpaceName = basisStream;
171 }
172 else
173 {
174 workSpaceName = basisStream + "_" + baseName;
175 }
176 return workSpaceName;
177 }
178
179 }