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 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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
44   * @version $Id: LocalCheckInCommand.java 524909 2007-04-02 20:02:44Z evenisse $
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              // Only copy files newer than in the repo
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                     // unknown file, skip
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 }