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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmFileStatus;
30  import org.apache.maven.scm.ScmResult;
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.command.status.StatusScmResult;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.hg.HgUtils;
37  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
38  import org.apache.maven.scm.provider.hg.command.HgConsumer;
39  import org.apache.maven.scm.provider.hg.command.status.HgStatusCommand;
40  import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  /**
44   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
45   * @author Olivier Lamy
46   *
47   */
48  public class HgCheckInCommand
49      extends AbstractCheckInCommand
50  {
51      /**
52       * {@inheritDoc}
53       */
54      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
55                                                        ScmVersion tag )
56          throws ScmException
57      {
58          if ( tag != null && !StringUtils.isEmpty( tag.getName() ) )
59          {
60              throw new ScmException( "This provider can't handle tags for this operation" );
61          }
62  
63  
64          File workingDir = fileSet.getBasedir();
65          String branchName = HgUtils.getCurrentBranchName( getLogger(), workingDir );
66          boolean differentOutgoingBranch =
67              repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName ) : false;
68  
69          // Get files that will be committed (if not specified in fileSet)
70          List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
71          List<File> files = fileSet.getFileList();
72          if ( files.isEmpty() )
73          { //Either commit all changes
74              HgStatusCommand statusCmd = new HgStatusCommand();
75              statusCmd.setLogger( getLogger() );
76              StatusScmResult status = statusCmd.executeStatusCommand( repo, fileSet );
77              List<ScmFile> statusFiles = status.getChangedFiles();
78              for ( ScmFile file : statusFiles )
79              {
80                  if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED
81                      || file.getStatus() == ScmFileStatus.MODIFIED )
82                  {
83                      commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
84                  }
85              }
86  
87          }
88          else
89          { //Or commit spesific files
90              for ( File file : files )
91              {
92                  commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
93              }
94          }
95  
96          // Commit to local branch
97          String[] commitCmd = new String[]{ HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message };
98          commitCmd = HgUtils.expandCommandLine( commitCmd, fileSet );
99          ScmResult result =
100             HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), commitCmd );
101 
102         // Push to parent branch if any
103         HgScmProviderRepository repository = (HgScmProviderRepository) repo;
104 
105         if ( repo.isPushChanges() )
106         {
107 
108             if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
109             {
110                 String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD,
111                     differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
112                     repository.getURI() };
113 
114                 result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
115             }
116 
117             return new CheckInScmResult( commitedFiles, result );
118         }
119 
120         return new CheckInScmResult( commitedFiles, result );
121     }
122 
123 
124 }