5

このコードを持つ

from dulwich.objects import Blob, Tree, Commit, parse_timezone
from dulwich.repo import Repo
from time import time

repo = Repo.init("myrepo", mkdir=True)
blob = Blob.from_string("my file content\n")
tree = Tree()
tree.add("spam", 0100644, blob.id)
commit = Commit()
commit.tree = tree.id


author = "Flav <foo@bar.com>"
commit.author = commit.committer = author
commit.commit_time = commit.author_time = int(time())
tz = parse_timezone('+0200')[0]
commit.commit_timezone = commit.author_timezone = tz
commit.encoding = "UTF-8"
commit.message = "initial commit"

o_sto = repo.object_store
o_sto.add_object(blob)
o_sto.add_object(tree)
o_sto.add_object(commit)

repo.refs["HEAD"] = commit.id

履歴にコミットしてしまいますが、作成されたファイルは削除待ちです(git statusそう言います)。

Aがgit checkout .修正します。

git checkout .私の質問は次のとおりです。ダルウィッチをプログラムで行うにはどうすればよいですか?

4

4 に答える 4

9

Git ステータスは、ファイルが作業コピーに存在しないため削除されたと表示しています。そのため、ファイルをチェックアウトするとステータスが修正されます。

ダルウィッチでは、高レベルの作業コピー クラスと関数はまだサポートされていないようです。ツリーとブロブ、およびオブジェクトのアンパックに対処する必要があります。

OK、挑戦しました: Dulwich で基本的なチェックアウトを行うことができました:

#get repository object of current directory
repo = Repo('.')
#get tree corresponding to the head commit
tree_id = repo["HEAD"].tree
#iterate over tree content, giving path and blob sha.
for entry in repo.object_store.iter_tree_contents(tree_id):
  path = entry.in_path(repo.path).path
  dulwich.file.ensure_dir_exists(os.path.split(path)[0])
  with open(path, 'wb') as file:
    #write blob's content to file
    file.write(repo[entry.sha].as_raw_string()) 

削除する必要のあるファイルを削除したり、インデックスを気にしたりしません。これに基づくより完全なコードについては、 Mark Mikofski の github プロジェクト
も参照してください。

于 2011-07-10T11:20:24.760 に答える
3

リリース0.8.4以降、メソッドを使用して可能になりましたdulwich.index.build_index_from_tree()

インデックスファイルとファイルシステム(作業コピー)の両方にツリーを書き込みます。これは、チェックアウトの非常に基本的な形式です。

注を参照してください

既存のインデックスは消去され、コンテンツは作業ディレクトリにマージされません。新鮮なクローンにのみ適しています

私はそれを次のコードで動作させることができました

from dulwich import index, repo
#get repository object of current directory
repo = repo.Repo('.')
indexfile = repo.index_path()
#we want to checkout HEAD
tree = repo["HEAD"].tree

index.build_index_from_tree(repo.path, indexfile, repo.object_store, tree)
于 2012-09-17T18:33:45.967 に答える
0
from dulwich.repo import Repo

repo = Repo.init('myrepo', mkdir=True)
f = open('myrepo/spam', 'w+')
f.write('my file content\n')
f.close()
repo.stage(['spam'])
repo.do_commit('initial commit', 'Flav <foo@bar.com>')

を見て発見dulwich/tests/test_repository.py:371。dulwich は強力ですが、残念ながらドキュメントが少し不足しています。

代わりにGitFile の使用を検討することもできます。

于 2011-07-10T11:29:42.407 に答える