View Javadoc
1   package org.apache.maven.scm.provider.local.command.checkin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
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.ScmVersion;
32  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
33  import org.apache.maven.scm.command.checkin.CheckInScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.local.command.LocalCommand;
36  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
37  import org.codehaus.plexus.util.FileUtils;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   *
43   */
44  public class LocalCheckInCommand
45      extends AbstractCheckInCommand
46      implements LocalCommand
47  {
48      /** {@inheritDoc} */
49      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
50                                                        ScmVersion version )
51          throws ScmException
52      {
53          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
54  
55          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
56          {
57              throw new ScmException( "The local scm doesn't support tags." );
58          }
59  
60          File root = new File( repository.getRoot() );
61  
62          String module = repository.getModule();
63  
64          File source = new File( root, module );
65  
66          File baseDestination = fileSet.getBasedir();
67  
68          if ( !baseDestination.exists() )
69          {
70              throw new ScmException(
71                  "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
72          }
73  
74          if ( !root.exists() )
75          {
76              throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
77          }
78  
79          if ( !source.exists() )
80          {
81              throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
82          }
83  
84          List<ScmFile> checkedInFiles = new ArrayList<ScmFile>();
85  
86          try
87          {
88              // Only copy files newer than in the repo
89              File repoRoot = new File( repository.getRoot(), repository.getModule() );
90  
91              List<File> files = fileSet.getFileList();
92  
93              if ( files.isEmpty() )
94              {
95                  @SuppressWarnings( "unchecked" )
96                  List<File> listFiles = FileUtils.getFiles( baseDestination, "**", null, false ); 
97                 
98                  files = listFiles;
99              }
100 
101             for ( File file : files )
102             {
103                 String path = file.getPath().replace( '\\', '/' );
104                 File repoFile = new File( repoRoot, path );
105                 file = new File( baseDestination, path );
106 
107                 ScmFileStatus status;
108 
109                 if ( repoFile.exists() )
110                 {
111                     String repoFileContents = FileUtils.fileRead( repoFile );
112 
113                     String fileContents = FileUtils.fileRead( file );
114 
115                     if ( getLogger().isDebugEnabled() )
116                     {
117                         getLogger().debug( "fileContents:" + fileContents );
118                         getLogger().debug( "repoFileContents:" + repoFileContents );
119                     }
120                     if ( fileContents.equals( repoFileContents ) )
121                     {
122                         continue;
123                     }
124 
125                     status = ScmFileStatus.CHECKED_IN;
126                 }
127                 else if ( repository.isFileAdded( path ) )
128                 {
129                     status = ScmFileStatus.CHECKED_IN;
130                 }
131                 else
132                 {
133                     if ( getLogger().isWarnEnabled() )
134                     {
135                         getLogger().warn( "skipped unknown file in checkin:" + path );
136                     }
137                     // unknown file, skip
138                     continue;
139                 }
140 
141                 FileUtils.copyFile( file, repoFile );
142                 ScmFile scmFile = new ScmFile( path, status);
143                 getLogger().info( scmFile.toString() );
144                 checkedInFiles.add( scmFile );
145             }
146         }
147         catch ( IOException ex )
148         {
149             throw new ScmException( "Error while checking in the files.", ex );
150         }
151 
152         return new CheckInScmResult( null, checkedInFiles );
153     }
154 }