简体   繁体   English

删除树并使用Rugged进行提交

[英]Removing a tree and committing with Rugged

I'm trying to remove an array of directories in a git repo and make 1 commit for each directory removed. 我正在尝试在git repo中删除目录数组,并对每个删除的目录进行1次提交。 I'm using Rugged and Gitlab_git (which is more or less just a wrapper around Rugged) and so far I've managed to do everything I need to except the actually deletion and commit. 我正在使用Rugged和Gitlab_git(或多或少只是Rugged的包装器),到目前为止,我已经设法完成了除实际删除和提交外所需的一切。

I don't see anything in the Rugged Readme that explains how to remove an entire tree/directory. 我在Rugged自述文件中看不到任何说明如何删除整个树/目录的内容。 I tried using their commit example for a blob and replacing a single file with a direcotry, but It didnt work 我尝试将他们的提交示例用于blob并用direcotry替换单个文件,但是它没有用

I also tried editing the code they had for the tree builder, but it added a commit to my history that showed every file ever in the repo having been added, and then left staging showing the same thing. 我还尝试编辑他们对于树生成器的代码,但是它向我的历史记录添加了一个提交,该提交显示了回购中曾经添加的每个文件,然后显示了相同的内容。 Nothing was deleted. 没有删除任何内容。

oid = repo.write("Removing folder", :blob)
builder = Rugged::Tree::Builder.new(repo)
builder << { :type => :blob, :name => "_delete", :oid => oid, :filemode => 0100644 }

options = {}
options[:tree] = builder.write

options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
options[:message] ||= "Making a commit via Rugged!"
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
options[:update_ref] = 'HEAD'

Rugged::Commit.create(repo, options)

Any suggestions? 有什么建议么? Im still a bit fuzzy on the git internals, so maybe thats my issue. 我对git内部仍然有点模糊,所以也许这就是我的问题。

The git index doesn't track directories explicitly, only their contents. git索引不会显式跟踪目录,仅会跟踪目录的内容。 To remove a directory, stage the removal of all of its contents. 要删除目录,请阶段性删除其所有内容。

You can make a Tree::Builder that is based on an existing tree in the repository, which you can then manipulate as you want. 您可以基于存储库中的现有树创建一个Tree::Builder ,然后可以根据需要进行操作。

If you already have the Commit object you want to have as your parent commit, then you can do this: 如果已经具有要作为父提交使用的Commit对象,则可以执行以下操作:

parent_commit = ... # e.g. this might be repo.head.target

# Create a Tree::Builder containing the current commit tree.
tree_builder = Rugged::Tree::Builder.new(repo, parent_commit.tree)

# Next remove the directory you want from the Tree::Builder.
tree_builder.remove('path/to/directory/to/remove')

# Now create a commit using the contents of the modified tree
# builder. (You might want to include the :update_ref option here
# depending on what you are trying to do - something like
# :update_ref => 'HEAD'.)
commit_data = {:message => "Remove directory with Rugged",
  :parents => [commit],
  :tree => tree_builder.write
}

Rugged::Commit.create(repo, commit_data)

This will create the commit in the repo with the directory removed, but might not update any branch pointers if you didn't use :update_ref . 这将在删除目录的仓库中创建提交,但如果不使用:update_ref ,则可能不会更新任何分支指针。

It also won't update your current working directory or index. 它还不会更新您当前的工作目录或索引。 If you want to update them you could reset to the new HEAD , but be careful about losing any work. 如果要更新它们,可以reset为新的HEAD ,但是请注意不要丢失任何工作。 Alternatively you could just remove the directory with Dir.rmdir , mimicking what you would do when removing the directory directly. 或者,您可以使用Dir.rmdir删除目录,模仿直接删除目录时的操作。

Check out the docs for more info, especially Tree::Builder and Commit.create . 查看文档以获取更多信息,尤其是Tree::BuilderCommit.create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM