/** * Show only commits that affect any of the specified paths. The path must * either name a file or a directory exactly and use <code>/</code> (slash) * as separator. Note that regex expressions or wildcards are not supported. * * @param path * a repository-relative path (with <code>/</code> as separator) * @return {@code this} */ public LogCommand addPath(String path) { checkCallable(); pathFilters.add(PathFilter.create(path)); return this; }
/** * Create a new tree filter for a user supplied path. * <p> * Path strings are relative to the root of the repository. If the user's * input should be assumed relative to a subdirectory of the repository the * caller must prepend the subdirectory's path prior to creating the filter. * <p> * Path strings use '/' to delimit directories on all platforms. * * @param path * the path to filter on. Must not be the empty string. All * trailing '/' characters will be trimmed before string's length * is checked or is used as part of the constructed filter. * @param cfg * diff config specifying rename detection options. * @return a new filter for the requested path. * @throws java.lang.IllegalArgumentException * the path supplied was the empty string. * @since 3.0 */ public static FollowFilter create(String path, DiffConfig cfg) { return new FollowFilter(PathFilter.create(path), cfg); }
/** * Create a collection of path filters from Java strings. * <p> * Path strings are relative to the root of the repository. If the user's * input should be assumed relative to a subdirectory of the repository the * caller must prepend the subdirectory's path prior to creating the filter. * <p> * Path strings use '/' to delimit directories on all platforms. * <p> * Paths may appear in any order within the collection. Sorting may be done * internally when the group is constructed if doing so will improve path * matching performance. * * @param paths * the paths to test against. Must have at least one entry. * @return a new filter for the list of paths supplied. */ public static TreeFilter createFromStrings(Collection<String> paths) { if (paths.isEmpty()) throw new IllegalArgumentException( JGitText.get().atLeastOnePathIsRequired); final PathFilter[] p = new PathFilter[paths.size()]; int i = 0; for (String s : paths) p[i++] = PathFilter.create(s); return create(p); }
@Signature public Memory diff(ArrayMemory settings) throws GitAPIException { DiffCommand command = getWrappedObject().diff(); if (settings != null) { command.setCached(settings.valueOfIndex("cached").toBoolean()); Memory contextLines = settings.valueOfIndex("contextLines"); if (contextLines.isNotNull()) { command.setContextLines(contextLines.toInteger()); } Memory destPrefix = settings.valueOfIndex("destPrefix"); if (destPrefix.isNotNull()) { command.setDestinationPrefix(destPrefix.toString()); } Memory sourcePrefix = settings.valueOfIndex("sourcePrefix"); if (sourcePrefix.isNotNull()) { command.setSourcePrefix(sourcePrefix.toString()); } command.setShowNameAndStatusOnly(settings.valueOfIndex("showNameAndStatusOnly").toBoolean()); Memory pathFilter = settings.valueOfIndex("pathFilter"); if (pathFilter.isNotNull()) { command.setPathFilter(PathFilter.create(pathFilter.toString())); } } List<DiffEntry> call = command.call(); return GitUtils.valueOfDiffEntries(call); }
/** * Create a collection of path filters from Java strings. * <p> * Path strings are relative to the root of the repository. If the user's * input should be assumed relative to a subdirectory of the repository the * caller must prepend the subdirectory's path prior to creating the filter. * <p> * Path strings use '/' to delimit directories on all platforms. * <p> * Paths may appear in any order. Sorting may be done internally when the * group is constructed if doing so will improve path matching performance. * * @param paths * the paths to test against. Must have at least one entry. * @return a new filter for the paths supplied. */ public static TreeFilter createFromStrings(String... paths) { if (paths.length == 0) throw new IllegalArgumentException( JGitText.get().atLeastOnePathIsRequired); final int length = paths.length; final PathFilter[] p = new PathFilter[length]; for (int i = 0; i < length; i++) p[i] = PathFilter.create(paths[i]); return create(p); }
/** * Is the configured already a submodule in the index? * * @return true if submodule exists in index, false otherwise * @throws java.io.IOException */ protected boolean submoduleExists() throws IOException { TreeFilter filter = PathFilter.create(path); try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) { return w.setFilter(filter).next(); } }
private void seek(String path) throws IOException { if (!path.equals(current)) { iterator.reset(); tw.reset(); tw.addTree(iterator); tw.setFilter(PathFilter.create(path)); current = path; if (!tw.next()) throw new FileNotFoundException(path); ptr = tw.getTree(0, WorkingTreeIterator.class); if (ptr == null) throw new FileNotFoundException(path); } } }
/** * Check if this path is a submodule by checking the index, which is what * git submodule deinit checks. * * @param path * path of the submodule * * @return {@code true} if path exists and is a submodule in index, * {@code false} otherwise * @throws IOException */ private boolean submoduleExists(String path) throws IOException { TreeFilter filter = PathFilter.create(path); try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) { return w.setFilter(filter).next(); } }
/** * Create a blame generator for the repository and path (relative to * repository) * * @param repository * repository to access revision data from. * @param path * initial path of the file to start scanning (relative to the * repository). */ public BlameGenerator(Repository repository, String path) { this.repository = repository; this.resultPath = PathFilter.create(path); idBuf = new MutableObjectId(); setFollowFileRenames(true); initRevPool(false); remaining = -1; }
private static void printDirectory(Repository repository, RevTree tree) throws IOException { // look at directory, this has FileMode.TREE try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("src")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected folder 'src'"); } // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true FileMode fileMode = treeWalk.getFileMode(0); System.out.println("src: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode); } }
private static void printDirectory(Repository repository, RevTree tree) throws IOException { // look at directory, this has FileMode.TREE try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("src")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected folder 'src'"); } // FileMode now indicates that this is a directory, i.e. FileMode.TREE.equals(fileMode) holds true FileMode fileMode = treeWalk.getFileMode(0); System.out.println("src: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode); } }
private static int countLinesOfFileInCommit(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // load the content of the file into a stream ByteArrayOutputStream stream = new ByteArrayOutputStream(); loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size(); } } } }
private static int countLinesOfFileInCommit(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // load the content of the file into a stream ByteArrayOutputStream stream = new ByteArrayOutputStream(); loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size(); } } } }
private static void printFile(Repository repository, RevTree tree) throws IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
private static void printFile(Repository repository, RevTree tree) throws IOException { // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(false); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } // FileMode specifies the type of file, FileMode.REGULAR_FILE for normal file, FileMode.EXECUTABLE_FILE for executable bit // set FileMode fileMode = treeWalk.getFileMode(0); ObjectLoader loader = repository.open(treeWalk.getObjectId(0)); System.out.println("README.md: " + getFileMode(fileMode) + ", type: " + fileMode.getObjectType() + ", mode: " + fileMode + " size: " + loader.getSize()); } }
treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'");
treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'");
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063"); // then the porcelain diff-command returns a list of diff entries try (Git git = new Git(repository)) { List<DiffEntry> diff = git.diff(). setOldTree(oldTreeParser). setNewTree(newTreeParser). setPathFilter(PathFilter.create("README.md")). // to filter on Suffix use the following instead //setPathFilter(PathSuffixFilter.create(".java")). call(); for (DiffEntry entry : diff) { System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId()); try (DiffFormatter formatter = new DiffFormatter(System.out)) { formatter.setRepository(repository); formatter.format(entry); } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063"); // then the porcelain diff-command returns a list of diff entries try (Git git = new Git(repository)) { List<DiffEntry> diff = git.diff(). setOldTree(oldTreeParser). setNewTree(newTreeParser). setPathFilter(PathFilter.create("README.md")). // to filter on Suffix use the following instead //setPathFilter(PathSuffixFilter.create(".java")). call(); for (DiffEntry entry : diff) { System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId()); try (DiffFormatter formatter = new DiffFormatter(System.out)) { formatter.setRepository(repository); formatter.format(entry); } } } } }
private boolean processOne(Candidate n) throws IOException { RevCommit parent = n.getParent(0); if (parent == null) return split(n.getNextCandidate(0), n); revPool.parseHeaders(parent); if (find(parent, n.sourcePath)) { if (idBuf.equals(n.sourceBlob)) return blameEntireRegionOnParent(n, parent); return splitBlameWithParent(n, parent); } if (n.sourceCommit == null) return result(n); DiffEntry r = findRename(parent, n.sourceCommit, n.sourcePath); if (r == null) return result(n); if (0 == r.getOldId().prefixCompare(n.sourceBlob)) { // A 100% rename without any content change can also // skip directly to the parent. n.sourceCommit = parent; n.sourcePath = PathFilter.create(r.getOldPath()); push(n); return false; } Candidate next = n.create(getRepository(), parent, PathFilter.create(r.getOldPath())); next.sourceBlob = r.getOldId().toObjectId(); next.renameScore = r.getScore(); next.loadText(reader); return split(next, n); }