View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.remove;
20  
21  import java.net.URI;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
30  import org.apache.maven.scm.util.AbstractConsumer;
31  
32  /**
33   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
34   * @author Olivier Lamy
35   *
36   */
37  public class GitRemoveConsumer extends AbstractConsumer {
38      /**
39       * The pattern used to match deleted file lines
40       */
41      private static final Pattern REMOVED_PATTERN = Pattern.compile("^rm\\s'(.*)'");
42  
43      private final List<ScmFile> removedFiles = new ArrayList<>();
44  
45      private final URI relativeRepositoryPath;
46  
47      // ----------------------------------------------------------------------
48      // StreamConsumer Implementation
49      // ----------------------------------------------------------------------
50  
51      public GitRemoveConsumer() {
52          this(null);
53      }
54  
55      public GitRemoveConsumer(URI relativeRepositoryPath) {
56          this.relativeRepositoryPath = relativeRepositoryPath;
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      public void consumeLine(String line) {
63          if (line.length() <= 2) {
64              return;
65          }
66  
67          Matcher matcher = REMOVED_PATTERN.matcher(line);
68          if (matcher.matches()) {
69              String file = GitStatusConsumer.resolvePath(matcher.group(1), relativeRepositoryPath);
70              removedFiles.add(new ScmFile(file, ScmFileStatus.DELETED));
71          } else {
72              if (logger.isInfoEnabled()) {
73                  logger.info("could not parse line: " + line);
74              }
75  
76              return;
77          }
78      }
79  
80      public List<ScmFile> getRemovedFiles() {
81          return removedFiles;
82      }
83  }