1 package org.apache.maven.scm.provider.hg.command.changelog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.maven.scm.ChangeFile;
28 import org.apache.maven.scm.ChangeSet;
29 import org.apache.maven.scm.ScmFileStatus;
30 import org.apache.maven.scm.log.ScmLogger;
31 import org.apache.maven.scm.provider.hg.command.HgConsumer;
32
33
34
35
36
37 public class HgChangeLogConsumer
38 extends HgConsumer
39 {
40
41 private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
42
43 private static final String REVNO_TAG = "changeset: ";
44
45 private static final String TAG_TAG = "tag: ";
46
47 private static final String AUTHOR_TAG = "user: ";
48
49 private static final String TIME_STAMP_TOKEN = "date: ";
50
51 private static final String MESSAGE_TOKEN = "description:";
52
53 private static final String FILES_TOKEN = "files: ";
54
55 private String prevLine = "";
56
57 private String prevPrevLine = "";
58
59 private List<ChangeSet> logEntries = new ArrayList<ChangeSet>();
60
61 private ChangeSet currentChange;
62
63 private String currentRevision;
64
65 @SuppressWarnings( "unused" )
66 private String currentTag;
67
68 private String userDatePattern;
69
70 private boolean spoolingComments;
71
72 private List<String> currentComment = null;
73
74 public HgChangeLogConsumer( ScmLogger logger, String userDatePattern )
75 {
76 super( logger );
77
78 this.userDatePattern = userDatePattern;
79 }
80
81 public List<ChangeSet> getModifications()
82 {
83 return logEntries;
84 }
85
86
87 public void consumeLine( String line )
88 {
89
90
91
92 doConsume( null, line );
93 }
94
95
96 public void doConsume( ScmFileStatus status, String line )
97 {
98 String tmpLine = line;
99
100
101 if ( ( line.equals( "" ) && ( prevLine.equals( "" ) && prevPrevLine.equals( "" ) ) ) || currentComment == null )
102 {
103 if ( currentComment != null )
104 {
105 StringBuilder comment = new StringBuilder();
106 for ( int i = 0; i < currentComment.size() - 1; i++ )
107 {
108 comment.append( currentComment.get( i ) );
109 if ( i + 1 < currentComment.size() - 1 )
110 {
111 comment.append( '\n' );
112 }
113 }
114 currentChange.setComment( comment.toString() );
115
116 }
117
118 spoolingComments = false;
119
120
121 currentChange = new ChangeSet();
122 currentChange.setFiles( new ArrayList<ChangeFile>( 0 ) );
123 logEntries.add( currentChange );
124
125
126 currentComment = new ArrayList<String>();
127 currentRevision = "";
128 }
129
130 if ( spoolingComments )
131 {
132 currentComment.add( line );
133 }
134 else if ( line.startsWith( MESSAGE_TOKEN ) )
135 {
136 spoolingComments = true;
137 }
138 else if ( line.startsWith( REVNO_TAG ) )
139 {
140 tmpLine = line.substring( REVNO_TAG.length() );
141 tmpLine = tmpLine.trim();
142 currentRevision = tmpLine.substring( tmpLine.indexOf( ':' ) + 1 );
143 currentChange.setRevision( currentRevision );
144
145 }
146 else if ( line.startsWith( TAG_TAG ) )
147 {
148 tmpLine = line.substring( TAG_TAG.length() ).trim();
149 currentTag = tmpLine;
150 }
151 else if ( line.startsWith( AUTHOR_TAG ) )
152 {
153 tmpLine = line.substring( AUTHOR_TAG.length() );
154 tmpLine = tmpLine.trim();
155 currentChange.setAuthor( tmpLine );
156 }
157 else if ( line.startsWith( TIME_STAMP_TOKEN ) )
158 {
159
160 tmpLine = line.substring( TIME_STAMP_TOKEN.length() ).trim();
161 Date date = parseDate( tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH );
162 currentChange.setDate( date );
163 }
164 else if ( line.startsWith( FILES_TOKEN ) )
165 {
166 tmpLine = line.substring( FILES_TOKEN.length() ).trim();
167 String[] files = tmpLine.split( " " );
168 for ( int i = 0; i < files.length; i++ )
169 {
170 String file = files[i];
171 ChangeFile changeFile = new ChangeFile( file, currentRevision );
172 currentChange.addFile( changeFile );
173 }
174 }
175 else if ( line.length() > 0 )
176 {
177 if ( getLogger().isWarnEnabled() )
178 {
179 getLogger().warn( "Could not figure out: " + line );
180 }
181 }
182
183
184 prevLine = line;
185 prevPrevLine = prevLine;
186 }
187 }