View Javadoc

1   package org.apache.maven.scm.provider.hg.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.ScmResult;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
29  import org.apache.maven.scm.command.checkin.CheckInScmResult;
30  import org.apache.maven.scm.command.status.StatusScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.hg.HgUtils;
33  import org.apache.maven.scm.provider.hg.command.HgCommand;
34  import org.apache.maven.scm.provider.hg.command.HgConsumer;
35  import org.apache.maven.scm.provider.hg.command.status.HgStatusCommand;
36  import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  import java.io.File;
40  import java.util.ArrayList;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  /**
45   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
46   */
47  public class HgCheckInCommand
48      extends AbstractCheckInCommand
49  {
50  
51      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
52                                                        ScmVersion tag )
53          throws ScmException
54      {
55          if ( tag != null && !StringUtils.isEmpty( tag.getName() ) )
56          {
57              throw new ScmException( "This provider can't handle tags for this operation" );
58          }
59  
60          // Get files that will be committed (if not specified in fileSet)
61          List commitedFiles = new ArrayList();
62          File[] files = fileSet.getFiles();
63          if ( files.length == 0 )
64          { //Either commit all changes
65              HgStatusCommand statusCmd = new HgStatusCommand();
66              statusCmd.setLogger( getLogger() );
67              StatusScmResult status = statusCmd.executeStatusCommand( repo, fileSet );
68              List statusFiles = status.getChangedFiles();
69              for ( Iterator it = statusFiles.iterator(); it.hasNext(); )
70              {
71                  ScmFile file = (ScmFile) it.next();
72                  if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED ||
73                      file.getStatus() == ScmFileStatus.MODIFIED )
74                  {
75                      commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
76                  }
77              }
78  
79          }
80          else
81          { //Or commit spesific files
82              for ( int i = 0; i < files.length; i++ )
83              {
84                  commitedFiles.add( new ScmFile( files[i].getPath(), ScmFileStatus.CHECKED_IN ) );
85              }
86          }
87  
88          // Commit to local branch
89          String[] commitCmd = new String[]{HgCommand.COMMIT_CMD, HgCommand.MESSAGE_OPTION, message};
90          commitCmd = HgUtils.expandCommandLine( commitCmd, fileSet );
91          ScmResult result =
92              HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), commitCmd );
93  
94          // Push to parent branch if any
95          HgScmProviderRepository repository = (HgScmProviderRepository) repo;
96          if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
97          {
98              String[] push_cmd = new String[]{HgCommand.PUSH_CMD, repository.getURI()};
99              result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), push_cmd );
100         }
101 
102         return new CheckInScmResult( commitedFiles, result );
103     }
104 
105 
106 }