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  public class GitRemoveConsumer extends AbstractConsumer {
37      /**
38       * The pattern used to match deleted file lines.
39       */
40      private static final Pattern REMOVED_PATTERN = Pattern.compile("^rm\\s'(.*)'");
41  
42      private final List<ScmFile> removedFiles = new ArrayList<>();
43  
44      private final URI relativeRepositoryPath;
45  
46      // ----------------------------------------------------------------------
47      // StreamConsumer Implementation
48      // ----------------------------------------------------------------------
49  
50      public GitRemoveConsumer() {
51          this(null);
52      }
53  
54      public GitRemoveConsumer(URI relativeRepositoryPath) {
55          this.relativeRepositoryPath = relativeRepositoryPath;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public void consumeLine(String line) {
62          if (line.length() <= 2) {
63              return;
64          }
65  
66          Matcher matcher = REMOVED_PATTERN.matcher(line);
67          if (matcher.matches()) {
68              String file = GitStatusConsumer.resolvePath(matcher.group(1), relativeRepositoryPath);
69              removedFiles.add(new ScmFile(file, ScmFileStatus.DELETED));
70          } else {
71              if (logger.isInfoEnabled()) {
72                  logger.info("could not parse line: " + line);
73              }
74          }
75      }
76  
77      public List<ScmFile> getRemovedFiles() {
78          return removedFiles;
79      }
80  }