View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.hg.command.checkin;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.lang3.StringUtils;
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  
42  /**
43   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
44   * @author Olivier Lamy
45   */
46  public class HgCheckInCommand extends AbstractCheckInCommand {
47      /**
48       * {@inheritDoc}
49       */
50      protected CheckInScmResult executeCheckInCommand(
51              ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion tag) throws ScmException {
52          if (tag != null && !StringUtils.isEmpty(tag.getName())) {
53              throw new ScmException("This provider can't handle tags for this operation");
54          }
55  
56          File workingDir = fileSet.getBasedir();
57          String branchName = HgUtils.getCurrentBranchName(workingDir);
58          boolean differentOutgoingBranch =
59                  repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound(workingDir, branchName) : false;
60  
61          // Get files that will be committed (if not specified in fileSet)
62          List<ScmFile> commitedFiles = new ArrayList<>();
63          List<File> files = fileSet.getFileList();
64          if (files.isEmpty()) { // Either commit all changes
65              HgStatusCommand statusCmd = new HgStatusCommand();
66              StatusScmResult status = statusCmd.executeStatusCommand(repo, fileSet);
67              List<ScmFile> statusFiles = status.getChangedFiles();
68              for (ScmFile file : statusFiles) {
69                  if (file.getStatus() == ScmFileStatus.ADDED
70                          || file.getStatus() == ScmFileStatus.DELETED
71                          || file.getStatus() == ScmFileStatus.MODIFIED) {
72                      commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
73                  }
74              }
75  
76          } else { // Or commit spesific files
77              for (File file : files) {
78                  commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
79              }
80          }
81  
82          // Commit to local branch
83          String[] commitCmd = new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message};
84          commitCmd = HgUtils.expandCommandLine(commitCmd, fileSet);
85          ScmResult result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), commitCmd);
86  
87          // Push to parent branch if any
88          HgScmProviderRepository repository = (HgScmProviderRepository) repo;
89  
90          if (repo.isPushChanges()) {
91  
92              if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
93                  String[] pushCmd = new String[] {
94                      HgCommandConstants.PUSH_CMD,
95                      differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
96                      repository.getURI()
97                  };
98  
99                  result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
100             }
101 
102             return new CheckInScmResult(commitedFiles, result);
103         }
104 
105         return new CheckInScmResult(commitedFiles, result);
106     }
107 }