001 package org.apache.maven.scm.provider.jazz.command.update;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.scm.ScmFile;
023 import org.apache.maven.scm.ScmFileStatus;
024 import org.apache.maven.scm.log.ScmLogger;
025 import org.apache.maven.scm.provider.ScmProviderRepository;
026 import org.apache.maven.scm.provider.jazz.command.consumer.AbstractRepositoryConsumer;
027
028 import java.util.ArrayList;
029 import java.util.List;
030 import java.util.regex.Matcher;
031 import java.util.regex.Pattern;
032
033 /**
034 * Consume the output of the scm command for the "acept" operation.
035 *
036 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
037 */
038 public class JazzUpdateConsumer
039 extends AbstractRepositoryConsumer
040 {
041 /**
042 * The "Update" command status flag for a resource that has been added.
043 */
044 public static final String UPDATE_CMD_ADD_FLAG = "-a-";
045
046 /**
047 * The "Update" command status flag for when the content or properties of a file have been modified, or the
048 * properties of a directory have changed.
049 */
050 public static final String UPDATE_CMD_CHANGE_FLAG = "--c";
051
052 /**
053 * The "Update" command status flag for a resource that has been deleted.
054 */
055 public static final String UPDATE_CMD_DELETE_FLAG = "-d-";
056
057 /**
058 * The "Update" command status flag for a resource that has been renamed or moved.
059 */
060 public static final String UPDATE_CMD_MOVED_FLAG = "-m-";
061
062 private List<ScmFile> fUpdatedFiles = new ArrayList<ScmFile>();
063
064 /**
065 * Construct the JazzUpdateCommand consumer.
066 *
067 * @param repository The repository we are working with.
068 * @param logger The logger to use.
069 */
070 public JazzUpdateConsumer( ScmProviderRepository repository, ScmLogger logger )
071 {
072 super( repository, logger );
073 }
074
075 /**
076 * Process one line of output from the execution of the "scm xxxx" command.
077 *
078 * @param line The line of output from the external command that has been pumped to us.
079 * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
080 */
081 public void consumeLine( String line )
082 {
083 super.consumeLine( line );
084 if ( containsStatusFlag( line ) )
085 {
086 extractUpdatedFile( line );
087 }
088 }
089
090 private boolean containsStatusFlag( String line )
091 {
092 boolean containsStatusFlag = false;
093
094 if ( line.trim().length() > 3 )
095 {
096 String flag = line.trim().substring( 0, 3 );
097 if ( UPDATE_CMD_ADD_FLAG.equals( flag ) ||
098 UPDATE_CMD_CHANGE_FLAG.equals( flag ) ||
099 UPDATE_CMD_DELETE_FLAG.equals( flag ) ||
100 UPDATE_CMD_MOVED_FLAG.equals( flag ) )
101 {
102 containsStatusFlag = true;
103 }
104 }
105 return containsStatusFlag;
106 }
107
108 private void extractUpdatedFile( String line )
109 {
110 String filePath = "";
111 String flag = line.trim().substring( 0, 3 );
112 ScmFileStatus status = ScmFileStatus.UNKNOWN;
113
114 if ( UPDATE_CMD_ADD_FLAG.equals( flag ) )
115 {
116 status = ScmFileStatus.ADDED;
117 filePath = line.trim().substring( 4 );
118 }
119
120 if ( UPDATE_CMD_CHANGE_FLAG.equals( flag ) )
121 {
122 status = ScmFileStatus.UPDATED;
123 filePath = line.trim().substring( 4 );
124 }
125
126 if ( UPDATE_CMD_DELETE_FLAG.equals( flag ) )
127 {
128 status = ScmFileStatus.DELETED;
129 filePath = line.trim().substring( 4 );
130 }
131
132 // TODO - It looks like there is a defect in RTC 2.0 SCM for moved (the "moved from <path> is not right)
133 // TODO - Also it looks like if you rename a file, the output shows the old name as changed, however it should
134 // be marked as deleted. (see if this is the case in RTC 3.0)
135 if ( UPDATE_CMD_MOVED_FLAG.equals( flag ) )
136 {
137 status = ScmFileStatus.ADDED;
138 String pattern = "^" + UPDATE_CMD_MOVED_FLAG + "\\s(.*)\\s\\(moved\\sfrom\\s.*$";
139 Pattern r = Pattern.compile( pattern );
140 Matcher m = r.matcher( line.trim() );
141 if ( m.find() )
142 {
143 filePath = m.group( 1 );
144 }
145 }
146
147 fUpdatedFiles.add( new ScmFile( filePath, status ) );
148 }
149
150 public List<ScmFile> getUpdatedFiles()
151 {
152 return fUpdatedFiles;
153 }
154
155 }