How to find all commits using jgit, not just referenceable ones

旧巷老猫 提交于 2021-02-04 21:10:41

问题


I am trying to use jGit to get all commits in a repository, not just the ones I can reach via heads or tags, but all the ones that were not yet garbage collected. Is there a way to do this with jGit in an efficient manner?

Update to better describe the actual use-case

I am working on a FUSE based filesystem which provides a filesystem-view of the Git history, see https://github.com/centic9/JGitFS/ for a first version (Linux/Mac only).

With this I am providing "virtual" sub-directories for commits, i.e. I am creating a directory structure like the following

/commit
   00
     abcd..
     bcde..
   ae
     bdas..

And beneath the commit-id the virtual filesystem provides the source-files "as-of" that commit.

Refs/Tags are provided as symbolic links to the actual commit the HEAD of that ref/tag:

/branch
   master -> ../commit/00/abcd...
   bugfix -> ../commit/ae/bdas...
/tag
   version_1 -> ../commit/00/bcde...

In order to make this filesystem fast, I need a way to iterate all commits in a repository very quickly. Looking at each tag and ref separately as I do now is sub-optimal as this way I look at the same commits many times if refs share a common history (which they do almost always!).

Preferably I would like to get a simple list of all commits that are still available, not just ones that are part of a branch, this way you can even look at versions that are not reachable any more by refs/tags.


回答1:


If finding commits that are referenced via reflog is enough, use ReflogCommand (I recommend using JGit 3.0 once it's released, which should be on 2013-06-26).

If you want to also find commits that are not referenced by reflog anymore, you need something like git fsck. JGit does not yet have an implementation of that. It does have an implementation of git gc though, which also has to find unreferenced objects.

See the source code of GC.java in the JGit repository. What you could do is to call GC#repack(), after which all referenced objects should be in pack files. Then you could do something similar to GC#prune, which find the loose objects that are unreferenced. Please note that GC is currently internal (not API), so don't rely on it staying like this.



来源:https://stackoverflow.com/questions/17178432/how-to-find-all-commits-using-jgit-not-just-referenceable-ones

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!