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