Create a tree object from a temporary index using libgit2 - git2go

六眼飞鱼酱① 提交于 2019-12-24 11:31:11

问题


On the command line I can do the following:

$ touch foo

$ GIT_INDEX_FILE=/tmp/tmp.d95ehfhUOffoo/index git add -A

$ GIT_INDEX_FILE=/tmp/tmp.d95ehfhUOffoo/index git status -s
A  foo

$ git status -s
?? foo

$ GIT_INDEX_FILE=/tmp/tmp.d95ehfhUOffoo/index git write-tree
b8f7b1e052b441b53a969014803516bb7e681760

$ git cat-file -p b8f7b1e052b441b53a969014803516bb7e681760
100644 blob ae3fdc2989ae00d75ec106feadb78ed9f98ba41a  .gitignore
100644 blob 4e1e0d2f722485c7d284fb5cd7da855826e39b5a  .rspec
100644 blob 4bc0f1cdd31d348ddc0e91365c5be5a40104fa17  Dockerfile
100644 blob 7dc939ea79c2bd70d8d32416f9da8aa568029b05  Gemfile
100644 blob 145bb24613da12ffef73d5db34e89e2ea8e265ad  LICENSE.txt
100644 blob b8a804e12699a964dbeb97b7a632250192421294  README.md
100644 blob 809eb5616adcc0ebb78862099794489bd5b5b1a0  Rakefile
100644 blob a18114c31713746a33a2e70d9914d1ef3e781425  foo
040000 tree 3c8a789fc07c8f35fb96a7e77896cbbf2384e3d7  lib
040000 tree a5e7e5d6a85504f30912a8f65a498d17fe989c01  spec
100755 blob 0dd422b073bc1123cdf4979432822db773463537  test

Note that when I run git status using the repository index it shows that foo has not been staged.

So, how can I do this with git2go? One attempt so far:

func Worktree() (string, error) {

› repo, err := git.OpenRepository(".")
› if err != nil {
› › return "", err
› }

› index, err := git.NewIndex()
› if err != nil {
› › return "", err
› }

› err = index.AddAll([]string{"."}, git.IndexAddDefault, nil)
› if err != nil {
› › return "", err
› }

› treeOid, err := index.WriteTreeTo(repo)
› if err != nil {
› › return "", err
› }

› return treeOid.String(), nil
}

Results in:

Error:  Could not add paths to index. Index is not backed up by an existing repository.

How do I associate an in memory index with the repository?

There seem to be some libgit functions, such as git_index_open and git_index_read that have not been implemented in git2go. I've scanned a lot of code and read a lot of the libgit2 api.

Some pointers (no, not *pointers) would be great.


回答1:


The purely in-memory index you have created doesn't know about any repository, which is why you have to use WriteIndexTo() and give it a Repository. For the same reason, any method which wants to use a relative path to a file won't work, because the index isn't associated with a repository and thus cannot know where the files are. You'd need to use IndexEntry instead and fill in the details.

If you need the relative path methods, you can grab the index from the repository and simply not write it back to disc, but create a tree from it, just like you do with your repository-less one. You can then just ignore the changes you've done in-memory.

If you need to re-use the Index from that repository, there's git_index_read() to re-read from the version on-disc, though I think it might not be wrapped in git2go at this time.



来源:https://stackoverflow.com/questions/28246887/create-a-tree-object-from-a-temporary-index-using-libgit2-git2go

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