1 package org.apache.maven.scm.provider.jazz.command.update;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.scm.ScmFile;
23 import org.apache.maven.scm.ScmFileStatus;
24 import org.apache.maven.scm.log.ScmLogger;
25 import org.apache.maven.scm.provider.ScmProviderRepository;
26 import org.apache.maven.scm.provider.jazz.command.consumer.AbstractRepositoryConsumer;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 /**
34 * Consume the output of the scm command for the "acept" operation.
35 *
36 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
37 */
38 public class JazzUpdateConsumer
39 extends AbstractRepositoryConsumer
40 {
41 /**
42 * The "Update" command status flag for a resource that has been added.
43 */
44 public static final String UPDATE_CMD_ADD_FLAG = "-a-";
45
46 /**
47 * The "Update" command status flag for when the content or properties of a file have been modified, or the
48 * properties of a directory have changed.
49 */
50 public static final String UPDATE_CMD_CHANGE_FLAG = "--c";
51
52 /**
53 * The "Update" command status flag for a resource that has been deleted.
54 */
55 public static final String UPDATE_CMD_DELETE_FLAG = "-d-";
56
57 /**
58 * The "Update" command status flag for a resource that has been renamed or moved.
59 */
60 public static final String UPDATE_CMD_MOVED_FLAG = "-m-";
61
62 private List<ScmFile> fUpdatedFiles = new ArrayList<ScmFile>();
63
64 /**
65 * Construct the JazzUpdateCommand consumer.
66 *
67 * @param repository The repository we are working with.
68 * @param logger The logger to use.
69 */
70 public JazzUpdateConsumer( ScmProviderRepository repository, ScmLogger logger )
71 {
72 super( repository, logger );
73 }
74
75 /**
76 * Process one line of output from the execution of the "scm xxxx" command.
77 *
78 * @param line The line of output from the external command that has been pumped to us.
79 * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
80 */
81 public void consumeLine( String line )
82 {
83 super.consumeLine( line );
84 if ( containsStatusFlag( line ) )
85 {
86 extractUpdatedFile( line );
87 }
88 }
89
90 private boolean containsStatusFlag( String line )
91 {
92 boolean containsStatusFlag = false;
93
94 if ( line.trim().length() > 3 )
95 {
96 String flag = line.trim().substring( 0, 3 );
97 if ( UPDATE_CMD_ADD_FLAG.equals( flag ) || UPDATE_CMD_CHANGE_FLAG.equals( flag )
98 || UPDATE_CMD_DELETE_FLAG.equals( flag ) || UPDATE_CMD_MOVED_FLAG.equals( flag ) )
99 {
100 containsStatusFlag = true;
101 }
102 }
103 return containsStatusFlag;
104 }
105
106 private void extractUpdatedFile( String line )
107 {
108 String filePath = "";
109 String flag = line.trim().substring( 0, 3 );
110 ScmFileStatus status = ScmFileStatus.UNKNOWN;
111
112 if ( UPDATE_CMD_ADD_FLAG.equals( flag ) )
113 {
114 status = ScmFileStatus.ADDED;
115 filePath = line.trim().substring( 4 );
116 }
117
118 if ( UPDATE_CMD_CHANGE_FLAG.equals( flag ) )
119 {
120 status = ScmFileStatus.UPDATED;
121 filePath = line.trim().substring( 4 );
122 }
123
124 if ( UPDATE_CMD_DELETE_FLAG.equals( flag ) )
125 {
126 status = ScmFileStatus.DELETED;
127 filePath = line.trim().substring( 4 );
128 }
129
130 // TODO - It looks like there is a defect in RTC 2.0 SCM for moved (the "moved from <path> is not right)
131 // TODO - Also it looks like if you rename a file, the output shows the old name as changed, however it should
132 // be marked as deleted. (see if this is the case in RTC 3.0)
133 if ( UPDATE_CMD_MOVED_FLAG.equals( flag ) )
134 {
135 status = ScmFileStatus.ADDED;
136 String pattern = "^" + UPDATE_CMD_MOVED_FLAG + "\\s(.*)\\s\\(moved\\sfrom\\s.*$";
137 Pattern r = Pattern.compile( pattern );
138 Matcher m = r.matcher( line.trim() );
139 if ( m.find() )
140 {
141 filePath = m.group( 1 );
142 }
143 }
144
145 fUpdatedFiles.add( new ScmFile( filePath, status ) );
146 }
147
148 public List<ScmFile> getUpdatedFiles()
149 {
150 return fUpdatedFiles;
151 }
152
153 }