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 *
036 */
037public class GitRemoveConsumer extends AbstractConsumer {
038    /**
039     * The pattern used to match deleted file lines
040     */
041    private static final Pattern REMOVED_PATTERN = Pattern.compile("^rm\\s'(.*)'");
042
043    private final List<ScmFile> removedFiles = new ArrayList<>();
044
045    private final URI relativeRepositoryPath;
046
047    // ----------------------------------------------------------------------
048    // StreamConsumer Implementation
049    // ----------------------------------------------------------------------
050
051    public GitRemoveConsumer() {
052        this(null);
053    }
054
055    public GitRemoveConsumer(URI relativeRepositoryPath) {
056        this.relativeRepositoryPath = relativeRepositoryPath;
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    public void consumeLine(String line) {
063        if (line.length() <= 2) {
064            return;
065        }
066
067        Matcher matcher = REMOVED_PATTERN.matcher(line);
068        if (matcher.matches()) {
069            String file = GitStatusConsumer.resolvePath(matcher.group(1), relativeRepositoryPath);
070            removedFiles.add(new ScmFile(file, ScmFileStatus.DELETED));
071        } else {
072            if (logger.isInfoEnabled()) {
073                logger.info("could not parse line: " + line);
074            }
075
076            return;
077        }
078    }
079
080    public List<ScmFile> getRemovedFiles() {
081        return removedFiles;
082    }
083}