001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command.remove;
020
021import java.net.URI;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
030import org.apache.maven.scm.util.AbstractConsumer;
031
032/**
033 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
034 * @author Olivier Lamy
035 */
036public class GitRemoveConsumer extends AbstractConsumer {
037    /**
038     * The pattern used to match deleted file lines.
039     */
040    private static final Pattern REMOVED_PATTERN = Pattern.compile("^rm\\s'(.*)'");
041
042    private final List<ScmFile> removedFiles = new ArrayList<>();
043
044    private final URI relativeRepositoryPath;
045
046    // ----------------------------------------------------------------------
047    // StreamConsumer Implementation
048    // ----------------------------------------------------------------------
049
050    public GitRemoveConsumer() {
051        this(null);
052    }
053
054    public GitRemoveConsumer(URI relativeRepositoryPath) {
055        this.relativeRepositoryPath = relativeRepositoryPath;
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    public void consumeLine(String line) {
062        if (line.length() <= 2) {
063            return;
064        }
065
066        Matcher matcher = REMOVED_PATTERN.matcher(line);
067        if (matcher.matches()) {
068            String file = GitStatusConsumer.resolvePath(matcher.group(1), relativeRepositoryPath);
069            removedFiles.add(new ScmFile(file, ScmFileStatus.DELETED));
070        } else {
071            if (logger.isInfoEnabled()) {
072                logger.info("could not parse line: " + line);
073            }
074        }
075    }
076
077    public List<ScmFile> getRemovedFiles() {
078        return removedFiles;
079    }
080}