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.changelog;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Date;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmBranch;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.Command;
34  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
35  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
36  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
37  import org.apache.maven.scm.command.changelog.ChangeLogSet;
38  import org.apache.maven.scm.provider.ScmProviderRepository;
39  import org.apache.maven.scm.provider.hg.HgUtils;
40  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
41  
42  /**
43   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
44   * @author Olivier Lamy
45   */
46  public class HgChangeLogCommand extends AbstractChangeLogCommand implements Command {
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      protected ChangeLogScmResult executeChangeLogCommand(ChangeLogScmRequest request) throws ScmException {
52          final ScmVersion startVersion = request.getStartRevision();
53          final ScmVersion endVersion = request.getEndRevision();
54          final ScmFileSet fileSet = request.getScmFileSet();
55          final String datePattern = request.getDatePattern();
56          if (startVersion != null || endVersion != null) {
57              final ScmProviderRepository scmProviderRepository =
58                      request.getScmRepository().getProviderRepository();
59              return executeChangeLogCommand(scmProviderRepository, fileSet, startVersion, endVersion, datePattern);
60          }
61          return executeChangeLogCommand(
62                  fileSet, request.getStartDate(), request.getEndDate(), datePattern, request.getLimit());
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      protected ChangeLogScmResult executeChangeLogCommand(
69              ScmProviderRepository scmProviderRepository,
70              ScmFileSet fileSet,
71              Date startDate,
72              Date endDate,
73              ScmBranch branch,
74              String datePattern)
75              throws ScmException {
76          return executeChangeLogCommand(fileSet, startDate, endDate, datePattern, null);
77      }
78  
79      private ChangeLogScmResult executeChangeLogCommand(
80              ScmFileSet fileSet, Date startDate, Date endDate, String datePattern, Integer limit) throws ScmException {
81          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
82          StringBuilder dateInterval = new StringBuilder();
83          // TRICK: Mercurial 1.9.3 don't accept 1970-01-01
84          dateInterval.append(
85                  dateFormat.format(startDate == null ? new Date(1000L * 60 * 60 * 24) : startDate)); // From 2. Jan 1970
86          dateInterval.append(" to ");
87          dateInterval.append(dateFormat.format(endDate == null ? new Date() : endDate)); // Upto now
88  
89          List<String> cmd = new ArrayList<>();
90          cmd.addAll(Arrays.asList(
91                  HgCommandConstants.LOG_CMD, HgCommandConstants.TEMPLATE_OPTION,
92                  HgCommandConstants.TEMPLATE_FORMAT, HgCommandConstants.NO_MERGES_OPTION,
93                  HgCommandConstants.DATE_OPTION, dateInterval.toString()));
94  
95          if (limit != null && limit > 0) {
96              cmd.add(HgCommandConstants.LIMIT_OPTION);
97              cmd.add(Integer.toString(limit));
98          }
99  
100         HgChangeLogConsumer consumer = new HgChangeLogConsumer(datePattern);
101         ScmResult result = HgUtils.execute(consumer, fileSet.getBasedir(), cmd.toArray(new String[cmd.size()]));
102 
103         List<ChangeSet> logEntries = consumer.getModifications();
104         ChangeLogSet changeLogSet = new ChangeLogSet(logEntries, startDate, endDate);
105         return new ChangeLogScmResult(changeLogSet, result);
106     }
107 
108     @Override
109     protected ChangeLogScmResult executeChangeLogCommand(
110             ScmProviderRepository repository,
111             ScmFileSet fileSet,
112             ScmVersion startVersion,
113             ScmVersion endVersion,
114             String datePattern)
115             throws ScmException {
116         StringBuilder revisionInterval = new StringBuilder();
117         if (startVersion != null) {
118             revisionInterval.append(startVersion.getName());
119         }
120         revisionInterval.append(":");
121         if (endVersion != null) {
122             revisionInterval.append(endVersion.getName());
123         }
124 
125         String[] cmd = new String[] {
126             HgCommandConstants.LOG_CMD,
127             HgCommandConstants.TEMPLATE_OPTION,
128             HgCommandConstants.TEMPLATE_FORMAT,
129             HgCommandConstants.NO_MERGES_OPTION,
130             HgCommandConstants.REVISION_OPTION,
131             revisionInterval.toString()
132         };
133         HgChangeLogConsumer consumer = new HgChangeLogConsumer(datePattern);
134         ScmResult result = HgUtils.execute(consumer, fileSet.getBasedir(), cmd);
135 
136         List<ChangeSet> logEntries = consumer.getModifications();
137         Date startDate = null;
138         Date endDate = null;
139         if (!logEntries.isEmpty()) {
140             startDate = logEntries.get(0).getDate();
141             endDate = logEntries.get(logEntries.size() - 1).getDate();
142         }
143         ChangeLogSet changeLogSet = new ChangeLogSet(logEntries, startDate, endDate);
144         changeLogSet.setStartVersion(startVersion);
145         changeLogSet.setEndVersion(endVersion);
146         return new ChangeLogScmResult(changeLogSet, result);
147     }
148 }