1 package org.apache.maven.perforcelib;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.IOException;
22
23 import java.util.Date;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.maven.changelog.AbstractChangeLogGenerator;
28 import org.apache.tools.ant.types.Commandline;
29
30
31 /**
32 * A Perforce implementation of the {@link org.apache.maven.changelog.ChangeLog}
33 * interface.
34 *
35 * @author <a href="mailto:jim@crossleys.org">Jim Crossley</a>
36 * @version $Id:
37 */
38 public class PerforceChangeLogGenerator extends AbstractChangeLogGenerator
39 {
40 /** The position after the connection prefix, 'scm:perforce:' */
41 private static final int POS_PARAMS = 13;
42
43 /** Log */
44 private static final Log LOG =
45 LogFactory.getLog( PerforceChangeLogGenerator.class );
46
47 /**
48 * Constructs the appropriate command line to execute the perforce
49 * log command whose output is then later parsed.
50 *
51 * Repository connection syntax:
52 * scm:perforce[:host:port]://depot/projects/name/...
53 *
54 * @return the cvs command line to be executed.
55 */
56 protected Commandline getScmLogCommand()
57 {
58 String conn = getConnection();
59
60 if ( !conn.startsWith( "scm:perforce:" ) )
61 {
62 throw new IllegalArgumentException( "repository connection string"
63 + " does not specify 'perforce' as the scm" );
64 }
65
66 int lastColon = conn.lastIndexOf( ':' );
67 String fileSpec = conn.substring( lastColon + 1 );
68 String p4port =
69 ( POS_PARAMS > lastColon ) ? null
70 : conn.substring( POS_PARAMS, lastColon );
71
72 Commandline command = new Commandline();
73
74 command.setExecutable( "p4" );
75
76 if ( p4port != null )
77 {
78 command.createArgument().setValue( "-p" );
79 command.createArgument().setValue( p4port );
80 }
81
82 command.createArgument().setValue( "filelog" );
83 command.createArgument().setValue( "-tl" );
84 command.createArgument().setValue( fileSpec );
85
86 return command;
87 }
88
89 /**
90 * The Perforce filelog doesn't support a revision, i.e. date,
91 * range
92 *
93 * @param before The starting point.
94 * @param to The ending point.
95 * @return An empty string
96 */
97 protected String getScmDateArgument( Date before, Date to )
98 {
99 return "";
100 }
101
102 /**
103 * Handle ChangeLogParser IOExceptions.
104 *
105 * @param ioe The IOException thrown.
106 * @throws IOException If the handler doesn't wish to handle the
107 * exception.
108 */
109 protected void handleParserException( IOException ioe )
110 throws IOException
111 {
112 if ( ( ioe.getMessage().indexOf( "CreateProcess" ) != -1 )
113 || ( ioe.getMessage().indexOf( "p4: not found" ) != -1 ) )
114 {
115
116 LOG.error( "Unable to find p4 executable. Changelog will be empty" );
117 }
118 else
119 {
120 throw ioe;
121 }
122 }
123
124 /**
125 * @see AbstractChangeLogGenerator#getScmTagArgument(String, String)
126 */
127 protected String getScmTagArgument( String tagStart, String tagEnd )
128 {
129 throw new UnsupportedOperationException(
130 "This plugin currently does not support generating logs from tags with Perforce." );
131 }
132 }