Grit:显示 master 中的所有文件
我正在尝试使用 Grit 写入 Git 存储库。我可以轻松地创建一个存储库并进行提交:
repo = Repo.init_bare("grit.git")
index = Index.new(repo)
index.add('myfile.txt', 'This is the content')
index.commit('first commit')
我还可以轻松地进行第二次提交,使用第一次提交作为父级:
index.add('myotherfile.txt', 'This is some other content')
index.commit("second commit", [repo.commits.first])
但是现在如何在不遍历整个提交历史记录的情况下获取这两个文件的内容?难道没有更智能的方法来获取存储库中文件的当前状态吗?
I'm trying to wrap my head around using Grit to write to a Git repository. I can easily create a repo and make a commit:
repo = Repo.init_bare("grit.git")
index = Index.new(repo)
index.add('myfile.txt', 'This is the content')
index.commit('first commit')
I can also easily make the second commit, using the first commit as parent:
index.add('myotherfile.txt', 'This is some other content')
index.commit("second commit", [repo.commits.first])
But now how do I get the content of those 2 files without traversing through the entire commit history? Isn't there a smarter way for me to get the current state of the files in a repo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具体来说, tree 方法(可以接受任何提交,但默认为master) 返回一个 树。 Tree 有一个方便的 / 方法,它返回一个 Blob 或 Tree,具体取决于您传入的文件名。最后,Blob 有一个 data 返回确切数据的方法。
编辑:如果您想要存储库中所有文件名的列表(这可能是一项昂贵的操作),一种方法是:
这假设所有内容都被跟踪。如果您不确定,可以根据
untracked
属性进行过滤。Specifically, the tree method (which can take any commit, but defaults to master) returns a Tree. Tree has a convenient / method which returns a Blob or Tree depending what filename you pass in. Finally, Blob has a data method that returns the exact data.
EDIT: If you want a list of all the filenames in the repo (which may be an expensive operation), one way is:
This assumes everything is tracked. If you're not sure, you can filter on the
untracked
attribute.