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   */
47  public class HgCheckInCommand extends AbstractCheckInCommand {
48      /**
49       * {@inheritDoc}
50       */
51      protected CheckInScmResult executeCheckInCommand(
52              ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion tag) throws ScmException {
53          if (tag != null && !StringUtils.isEmpty(tag.getName())) {
54              throw new ScmException("This provider can't handle tags for this operation");
55          }
56  
57          File workingDir = fileSet.getBasedir();
58          String branchName = HgUtils.getCurrentBranchName(workingDir);
59          boolean differentOutgoingBranch =
60                  repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound(workingDir, branchName) : false;
61  
62          // Get files that will be committed (if not specified in fileSet)
63          List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
64          List<File> files = fileSet.getFileList();
65          if (files.isEmpty()) { // Either commit all changes
66              HgStatusCommand statusCmd = new HgStatusCommand();
67              StatusScmResult status = statusCmd.executeStatusCommand(repo, fileSet);
68              List<ScmFile> statusFiles = status.getChangedFiles();
69              for (ScmFile file : statusFiles) {
70                  if (file.getStatus() == ScmFileStatus.ADDED
71                          || file.getStatus() == ScmFileStatus.DELETED
72                          || file.getStatus() == ScmFileStatus.MODIFIED) {
73                      commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
74                  }
75              }
76  
77          } else { // Or commit spesific files
78              for (File file : files) {
79                  commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
80              }
81          }
82  
83          // Commit to local branch
84          String[] commitCmd = new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message};
85          commitCmd = HgUtils.expandCommandLine(commitCmd, fileSet);
86          ScmResult result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), commitCmd);
87  
88          // Push to parent branch if any
89          HgScmProviderRepository repository = (HgScmProviderRepository) repo;
90  
91          if (repo.isPushChanges()) {
92  
93              if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
94                  String[] pushCmd = new String[] {
95                      HgCommandConstants.PUSH_CMD,
96                      differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
97                      repository.getURI()
98                  };
99  
100                 result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
101             }
102 
103             return new CheckInScmResult(commitedFiles, result);
104         }
105 
106         return new CheckInScmResult(commitedFiles, result);
107     }
108 }