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.util.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.apache.maven.scm.ChangeFile;
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.provider.hg.command.HgConsumer;
30  
31  /**
32   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
33   * @author <a href="mailto:hr.mohr@gmail.com">Mads Mohr Christensen</a>
34   */
35  public class HgChangeLogConsumer extends HgConsumer {
36  
37      private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
38  
39      private static final String REVNO_TAG = "changeset:";
40  
41      private static final String TAG_TAG = "tag:";
42  
43      private static final String BRANCH_TAG = "branch:";
44  
45      private static final String AUTHOR_TAG = "user:";
46  
47      private static final String TIME_STAMP_TOKEN = "date:";
48  
49      private static final String MESSAGE_TOKEN = "description:";
50  
51      private static final String FILES_TOKEN = "files:";
52  
53      private List<ChangeSet> logEntries = new ArrayList<>();
54  
55      private ChangeSet currentChange;
56  
57      private String currentRevision;
58  
59      @SuppressWarnings("unused")
60      private String currentBranch; // don't know what to do with this
61  
62      private String userDatePattern;
63  
64      public HgChangeLogConsumer(String userDatePattern) {
65          this.userDatePattern = userDatePattern;
66      }
67  
68      public List<ChangeSet> getModifications() {
69          return logEntries;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void consumeLine(String line) {
76          // override default behaviour which tries to pick through things for some standard messages.  that
77          // does not apply here
78          String trimmedLine = line.trim();
79          doConsume(null, trimmedLine);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public void doConsume(ScmFileStatus status, String line) {
86          String tmpLine;
87  
88          // new changeset
89          if (line.startsWith(REVNO_TAG)) {
90              // Init a new changeset
91              currentChange = new ChangeSet();
92              currentChange.setFiles(new ArrayList<>(0));
93              logEntries.add(currentChange);
94  
95              // parse revision
96              tmpLine = line.substring(REVNO_TAG.length()).trim();
97              currentRevision = tmpLine.substring(tmpLine.indexOf(':') + 1);
98              currentChange.setRevision(currentRevision);
99          } else if (line.startsWith(BRANCH_TAG)) {
100             tmpLine = line.substring(BRANCH_TAG.length()).trim();
101             currentBranch = tmpLine;
102         } else if (line.startsWith(AUTHOR_TAG)) {
103             tmpLine = line.substring(AUTHOR_TAG.length()).trim();
104             currentChange.setAuthor(tmpLine);
105         } else if (line.startsWith(TIME_STAMP_TOKEN)) {
106             tmpLine = line.substring(TIME_STAMP_TOKEN.length()).trim();
107             Date date = parseDate(tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH);
108             currentChange.setDate(date);
109         } else if (line.startsWith(TAG_TAG)) {
110             tmpLine = line.substring(TAG_TAG.length()).trim();
111             currentChange.addTag(tmpLine);
112         } else if (line.startsWith(FILES_TOKEN)) {
113             tmpLine = line.substring(FILES_TOKEN.length()).trim();
114             String[] files = tmpLine.split(" ");
115             for (int i = 0; i < files.length; i++) {
116                 String file = files[i];
117                 ChangeFile changeFile = new ChangeFile(file, currentRevision);
118                 currentChange.addFile(changeFile);
119             }
120         } else if (line.startsWith(MESSAGE_TOKEN)) {
121             currentChange.setComment("");
122         } else {
123             StringBuilder comment = new StringBuilder(currentChange.getComment());
124             comment.append(line);
125             currentChange.setComment(comment.toString());
126         }
127     }
128 }