如何做"git grep -e< pattern>"与jgit? [英] How to do "git grep -e <pattern>" with jgit?

查看:82
本文介绍了如何做"git grep -e< pattern>"与jgit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"git grep -e"来查找与内容中的模式匹配的文件.我查看了jgit的索引,找不到"grep",但最接近的是PatternMatchRevFilter.这类似于"git grep"在做什么吗?

I am using "git grep -e " to find files that matches the pattern in content. I looked in the index of jgit, and can't find "grep", but the closest is PatternMatchRevFilter. Is this similar to what "git grep" is doing?

在JGit官方用户指南中,它说"TODO谈论过滤器".:)有人有使用此过滤器的示例吗?

In the official JGit user guide, it says "TODO talk about filters". :) Does someone have an example of how to use this filter?

谢谢!

Jason

ps.这可能是一个单独的问题-如何为搜索指定分支?

ps. this might be a separate question - how can I specify a branch for the search?

推荐答案

首先,PatternMatchRevFilter不是您想要的.RevFilter用于在行走过程中选择某些修订(提交).因此,它等效于 git log --grep = pattern .

First off, PatternMatchRevFilter is not the one you want. A RevFilter is for selecting certain revisions (commits) during walking. So it's the equivalent of git log --grep=pattern.

您想要的是走一棵单一修订版本的树并阅读blob内容.

What you want is to walk a tree of a single revision and read the blob contents.

还没有一个简单易用的API可以等效于 git grep .您已经结合了较低级别的API:

There is not yet a simple to use API for the equivalent of git grep. You have combine the lower-level APIs:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.TreeWalk;

public class Grep {

    private final Repository repository;
    private final Pattern pattern;

    private final String revName;

    public Grep(Repository repository, Pattern pattern, String revName) {
        this.repository = repository;
        this.pattern = pattern;
        this.revName = revName;
    }

    public void grepPrintingResults() throws IOException {
        ObjectReader objectReader = repository.newObjectReader();
        try {
            ObjectId commitId = repository.resolve(revName);
            impl(objectReader, commitId);
        } finally {
            objectReader.release();
        }
    }

    private void impl(ObjectReader objectReader, ObjectId commitId)
            throws IOException {

        TreeWalk treeWalk = new TreeWalk(objectReader);
        RevWalk revWalk = new RevWalk(objectReader);
        RevCommit commit = revWalk.parseCommit(commitId);

        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        treeParser.reset(objectReader, commit.getTree());

        int treeIndex = treeWalk.addTree(treeParser);
        treeWalk.setRecursive(true);

        while (treeWalk.next()) {
            AbstractTreeIterator it = treeWalk.getTree(treeIndex,
                    AbstractTreeIterator.class);
            ObjectId objectId = it.getEntryObjectId();
            ObjectLoader objectLoader = objectReader.open(objectId);

            if (!isBinary(objectLoader.openStream())) {
                List<String> matchedLines = getMatchedLines(objectLoader
                        .openStream());
                if (!matchedLines.isEmpty()) {
                    String path = it.getEntryPathString();
                    for (String matchedLine : matchedLines) {
                        System.out.println(path + ":" + matchedLine);
                    }
                }
            }
        }
    }

    private List<String> getMatchedLines(InputStream stream) throws IOException {
        BufferedReader buf = null;
        try {
            List<String> matchedLines = new ArrayList<String>();
            InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
            buf = new BufferedReader(reader);
            String line;
            while ((line = buf.readLine()) != null) {
                Matcher m = pattern.matcher(line);
                if (m.find()) {
                    matchedLines.add(line);
                }
            }
            return matchedLines;
        } finally {
            if (buf != null) {
                buf.close();
            }
        }
    }

    private static boolean isBinary(InputStream stream) throws IOException {
        try {
            return RawText.isBinary(stream);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                // Ignore, we were just reading
            }
        }
    }
}

像这样使用它:

Grep grep = new Grep(repository, Pattern.compile("test"), "HEAD");
grep.grepPrintingResults();

这篇关于如何做"git grep -e&lt; pattern&gt;"与jgit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆