How to “cat” a file in JGit?

后端 未结 7 603
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 04:25

A while back I was looking for an embeddable distributed version control system in Java, and I think I have found it in JGit, which is a pure Java implementation of git. How

相关标签:
7条回答
  • 2020-12-01 05:16

    I have started writing a library called gitective that contains many helpers for working with blobs, commits, and trees using JGit and is MIT-licensed and available on GitHub.

    Get content of file in HEAD commit

    Repository repo = new FileRepository("/repos/project/.git");
    String content = BlobUtils.getHeadContent(repo, "src/Buffer.java");
    

    Get content of a file on a branch

    Repository repo = new FileRepository("/repos/project/.git");
    String content = BlobUtils.getContent(repo, "master", "src/Buffer.java");
    

    Diff two files

    Repository repo = new FileRepository("/repos/project/.git");
    ObjectId current = BlobUtils.getId(repo, "master", "Main.java");
    ObjectId previous = BlobUtils.getId(repo, "master~1", "Main.java");
    Collection<Edit> edit = BlobUtils.diff(repo, previous, current);
    

    More examples of utilities provided are detailed in the README.

    0 讨论(0)
提交回复
热议问题