View Javadoc
1   /*
2    * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.treewalk.filter;
11  
12  import java.io.IOException;
13  
14  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
15  import org.eclipse.jgit.errors.MissingObjectException;
16  import org.eclipse.jgit.treewalk.TreeWalk;
17  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
18  
19  /**
20   * Skip {@link org.eclipse.jgit.treewalk.WorkingTreeIterator} entries that
21   * appear in gitignore files.
22   */
23  public class NotIgnoredFilter extends TreeFilter {
24  	private final int index;
25  
26  	/**
27  	 * Construct a filter to ignore paths known to a particular iterator.
28  	 *
29  	 * @param workdirTreeIndex
30  	 *            index of the workdir tree in the tree walk
31  	 */
32  	public NotIgnoredFilter(int workdirTreeIndex) {
33  		this.index = workdirTreeIndex;
34  	}
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	public boolean include(TreeWalk tw) throws MissingObjectException,
39  			IncorrectObjectTypeException, IOException {
40  		WorkingTreeIterator i = tw.getTree(index, WorkingTreeIterator.class);
41  		return i == null || !i.isEntryIgnored();
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public boolean shouldBeRecursive() {
47  		return false;
48  	}
49  
50  	/** {@inheritDoc} */
51  	@Override
52  	public TreeFilter clone() {
53  		// immutable
54  		return this;
55  	}
56  
57  	/** {@inheritDoc} */
58  	@SuppressWarnings("nls")
59  	@Override
60  	public String toString() {
61  		return "NotIgnored(" + index + ")";
62  	}
63  }