1 package org.apache.maven.scm.provider.local.command.checkin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
26 import org.apache.maven.scm.ScmVersion;
27 import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
28 import org.apache.maven.scm.command.checkin.CheckInScmResult;
29 import org.apache.maven.scm.provider.ScmProviderRepository;
30 import org.apache.maven.scm.provider.local.command.LocalCommand;
31 import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
32 import org.codehaus.plexus.util.FileUtils;
33 import org.codehaus.plexus.util.StringUtils;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Iterator;
40 import java.util.List;
41
42
43
44
45
46 public class LocalCheckInCommand
47 extends AbstractCheckInCommand
48 implements LocalCommand
49 {
50 protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
51 ScmVersion version )
52 throws ScmException
53 {
54 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
55
56 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
57 {
58 throw new ScmException( "The local scm doesn't support tags." );
59 }
60
61 File root = new File( repository.getRoot() );
62
63 String module = repository.getModule();
64
65 File source = new File( root, module );
66
67 File baseDestination = fileSet.getBasedir();
68
69 if ( !baseDestination.exists() )
70 {
71 throw new ScmException(
72 "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
73 }
74
75 if ( !root.exists() )
76 {
77 throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
78 }
79
80 if ( !source.exists() )
81 {
82 throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
83 }
84
85 List checkedInFiles = new ArrayList();
86
87 try
88 {
89
90 File repoRoot = new File( repository.getRoot(), repository.getModule() );
91
92 List files = Arrays.asList( fileSet.getFiles() );
93
94 if ( files.isEmpty() )
95 {
96 files = FileUtils.getFiles( baseDestination, "**", null, false );
97 }
98
99 Iterator it = files.iterator();
100
101 while ( it.hasNext() )
102 {
103 File file = (File) it.next();
104
105 String path = file.getPath().replace( '\\', '/' );
106 File repoFile = new File( repoRoot, path );
107 file = new File( baseDestination, path );
108
109 ScmFileStatus status;
110
111 if ( repoFile.exists() )
112 {
113 String repoFileContents = FileUtils.fileRead( repoFile );
114
115 String fileContents = FileUtils.fileRead( file );
116
117 if ( getLogger().isDebugEnabled() )
118 {
119 getLogger().debug( "fileContents:" + fileContents );
120 getLogger().debug( "repoFileContents:" + repoFileContents );
121 }
122 if ( fileContents.equals( repoFileContents ) )
123 {
124 continue;
125 }
126
127 status = ScmFileStatus.CHECKED_IN;
128 }
129 else if ( repository.isFileAdded( path ) )
130 {
131 status = ScmFileStatus.CHECKED_IN;
132 }
133 else
134 {
135 getLogger().warn( "skipped unknown file in checkin:" + path );
136
137 continue;
138 }
139
140 FileUtils.copyFile( file, repoFile );
141
142 System.err.println( new ScmFile( path, status ) );
143 checkedInFiles.add( new ScmFile( path, status ) );
144 }
145 }
146 catch ( IOException ex )
147 {
148 throw new ScmException( "Error while checking in the files.", ex );
149 }
150
151 return new CheckInScmResult( null, checkedInFiles );
152 }
153 }