【问题标题】:Removing a tree and committing with Rugged移除一棵树并使用 Rugged 提交
【发布时间】:2017-03-03 02:51:08
【问题描述】:

我正在尝试删除 git repo 中的目录数组,并为每个删除的目录进行 1 次提交。我正在使用 Rugged 和 Gitlab_git(它或多或少只是 Rugged 的​​一个包装器),到目前为止,除了实际删除和提交之外,我已经设法完成了我需要做的所有事情。

我在Rugged Readme 中没有看到任何解释如何删除整个树/目录的内容。我尝试将他们的提交示例用于 blob 并用目录替换单个文件,但它没有用

我还尝试编辑他们为树构建器提供的代码,但它在我的历史记录中添加了一个提交,显示回购中的 每个 文件已被添加,然后离开 staging 显示相同事物。什么都没有被删除。

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)

有什么建议吗?我对 git 内部仍然有点模糊,所以也许这就是我的问题。

【问题讨论】:

标签: ruby git gitlab libgit2 rugged


【解决方案1】:

git 索引不显式跟踪目录,只跟踪它们的内容。要删除目录,请分阶段删除其所有内容。

【讨论】:

    【解决方案2】:

    您可以基于存储库中的现有树创建Tree::Builder,然后您可以根据需要对其进行操作。

    如果您已经拥有想要作为父提交的 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)
    

    这将在 repo 中创建提交并删除目录,但如果您不使用 :update_ref,则可能不会更新任何分支指针。

    它也不会更新您当前的工作目录或索引。如果您想更新它们,您可以将reset 更新为新的HEAD,但要小心丢失任何工作。或者,您可以使用 Dir.rmdir 删除目录,模仿直接删除目录时的操作。

    查看docs 了解更多信息,尤其是Tree::BuilderCommit.create

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2014-12-06
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多