pygit2 / libgit2 AttributeError:' _pygit2.Reference'对象没有属性' oid'

时间:2014-06-30 03:35:26

标签: libgit2

我正在尝试创建一个存储库并向其提交文件,但是获取错误AttributeError:'_ pygit2.Reference'对象没有属性'oid'

欢迎任何建议。

(venv3.4.1) ubuntu@app:/var/www/app-/src/tests/api$ python test_git_repo.py 
Traceback (most recent call last):
  File "test_git_repo.py", line 35, in <module>
    add_file_to_repo()
  File "test_git_repo.py", line 31, in add_file_to_repo
    repo.create_commit('HEAD',author, committer,'Files saved by front end commit.', treeid, [repo.head.oid] )
AttributeError: '_pygit2.Reference' object has no attribute 'oid'
(venv3.4.1) ubuntu@app:/var/www/app-/src/tests/api$ cat test
cat: test: No such file or directory
(venv3.4.1) ubuntu@app:/var/www/app-/src/tests/api$ cat test_git_repo.py 
import pygit2
from time import time
import io
import sys

repo_id = 'my_repo01'
repo_location = '/usr/local/repo/' + repo_id
author = pygit2.Signature('Author Name', 'author@example.org', int(time()), 0)
committer = author

def create_repo():
    repo = pygit2.init_repository(repo_location, bare=True) # Creates a bare repository
    bld = repo.TreeBuilder()
    t = bld.write()
    c = repo.create_commit('HEAD', author, committer, 'Create master branch', t, [])

def add_file_to_repo():
    repo = pygit2.Repository(repo_location)
    head = repo.head
    f = io.BytesIO()
    f.write(b'hello')
    # create the file as blob in git
    blob_id = repo.write(pygit2.GIT_OBJ_BLOB, f.read()) 
    # add file to index/working area
    index = repo.index
    index.read()
    new_entry = pygit2.IndexEntry('path/to/my/file.txt', blob_id, pygit2.GIT_FILEMODE_BLOB)
    index.add(new_entry)
    treeid = index.write_tree()
    index.write() # write index to disk
    repo.create_commit('HEAD',author, committer,'Files saved by front end commit.', treeid, [repo.head.oid] )
    f.close()

create_repo()
add_file_to_repo()

(venv3.4.1) ubuntu@app:/var/www/app-/src/tests/api$ 

1 个答案:

答案 0 :(得分:2)

正如您在[0]的参考文档中所看到的,他们指向的对象或引用是通过target字段访问的。

[0] http://www.pygit2.org/references.html#the-reference-type