1

当使用坚固的删除文件来暂存提交时,提交的差异会出现关于哪些文件被删除的错误信息。应该存在的文件实际上仍然存在,并且项目的结果状态是正确的。只是差异报告不正确。

我可能会误解/误用Rugged::Tree::Builder,但除了差异之外,一切似乎都正确。请注意,我在这里留下了一个空树对象,但我不明白为什么这会影响这一点。

项目结构:

demo-project/ └── MasterDir ├── Directory1 │   ├── File1 │   ├── File2 │   └── File3 ├── Directory2 │   ├── File4 │   └── File5 ├── Directory3 │   └── File6 └── Directory4 └── File7

调用顺序

require 'rugged'

repo = Rugged::Repository.new("/Users/davetakahashi/demo-project")

# get the tree for "MasterDir/Directory1"
tree = repo.lookup(repo.head.target.tree.path("MasterDir/Directory1")[:oid])

# initialize a builder with the tree
builder = Rugged::Tree::Builder.new(tree)

# remove the files
builder.remove("File1")

builder.remove("File2")

builder.remove("File3")

# write the tree, saving the oid to write to parent tree
empty_directory_1_oid = builder.write(repo)

# get the tree for "MasterDir"
parent_tree = repo.lookup(repo.head.target.tree.path("MasterDir")[:oid])

# initialize a builder with it
builder = Rugged::Tree::Builder.new(parent_tree)

# add the updated entry for "Directory1"
builder << {:oid => empty_directory_1_oid, :filemode => 040000, :name => "Directory1", :type => :tree}

# write to repo, save oid
new_master_dir_oid = builder.write(repo)

# oid for "MasterDir" @ HEAD
old_master_dir_oid = repo.head.target.tree.path("MasterDir")[:oid]

# new MasterDir tree with empty Directory1
new_thing = repo.lookup(new_master_dir_oid)

# original MasterDir tree 
old_thing = repo.lookup(old_master_dir_oid)

# diff shows that every file in MasterDir was deleted
old_thing.diff(new_thing).deltas

irb 会话:

  [demo-project (master)]$ irb

  2.1.3 :001 > require 'rugged'
   => true 

  2.1.3 :002 > repo = Rugged::Repository.new("/Users/davetakahashi/demo-project")
   => #<Rugged::Repository:70270137753220 {path: "/Users/davetakahashi/demo-project/.git/"}> 

  2.1.3 :003 > tree = repo.lookup(repo.head.target.tree.path("MasterDir/Directory1")[:oid])
   => #<Rugged::Tree:70270137716900 {oid: abdee7bc13e432263b8da96776faa870a0b705b5}>
    <"File1" edd02898ce2deab5b06f9af79c97225427aa0202>
    <"File2" 2289c92f857605706b5bb1405e8b62b188777f36>
    <"File3" 248bce651b8c13085beb99c77b63d83a9866fbe5>

  2.1.3 :004 > builder = Rugged::Tree::Builder.new(tree)
   => #<Rugged::Tree::Builder:0x007fd215310a08> 

  2.1.3 :005 > builder.remove("File1")
   => true 

  2.1.3 :006 > builder.remove("File2")
   => true 

  2.1.3 :007 > builder.remove("File3")
   => true 

  2.1.3 :008 > empty_directory_1_oid = builder.write(repo)
   => "4b825dc642cb6eb9a060e54bf8d69288fbee4904" 

  2.1.3 :009 > parent_tree = repo.lookup(repo.head.target.tree.path("MasterDir")[:oid])
   => #<Rugged::Tree:70270137598260 {oid: 67e1a15042779fd7a0bb5f55517d15acb407a36a}>
    <"Directory1" abdee7bc13e432263b8da96776faa870a0b705b5>
    <"Directory2" ba23af1963e685c6385440ea1d76d92eb0fc4728>
    <"Directory3" eaea4f258ad3488468c52e068994c6289c9ee4ea>
    <"Directory4" 3344b4aa8fec8f28d189d5d2f05285e60f26cbd4>

  2.1.3 :010 > builder = Rugged::Tree::Builder.new(parent_tree)
   => #<Rugged::Tree::Builder:0x007fd2152d2730> 

  2.1.3 :011 > builder << {:oid => empty_directory_1_oid, :filemode => 040000, :name => "Directory1", :type => :tree}
   => nil 

  2.1.3 :012 > new_master_dir_oid = builder.write(repo)
   => "a1a2ebae492b845684bb53a01981b5710a9d1814" 

  2.1.3 :013 > old_master_dir_oid = repo.head.target.tree.path("MasterDir")[:oid]
   => "67e1a15042779fd7a0bb5f55517d15acb407a36a" 

  2.1.3 :014 > new_thing = repo.lookup(new_master_dir_oid)
   => #<Rugged::Tree:70270137454060 {oid: a1a2ebae492b845684bb53a01981b5710a9d1814}>
    <"Directory1" 4b825dc642cb6eb9a060e54bf8d69288fbee4904>
    <"Directory2" ba23af1963e685c6385440ea1d76d92eb0fc4728>
    <"Directory3" eaea4f258ad3488468c52e068994c6289c9ee4ea>
    <"Directory4" 3344b4aa8fec8f28d189d5d2f05285e60f26cbd4>

  2.1.3 :015 > old_thing = repo.lookup(old_master_dir_oid)
   => #<Rugged::Tree:70270137436000 {oid: 67e1a15042779fd7a0bb5f55517d15acb407a36a}>
    <"Directory1" abdee7bc13e432263b8da96776faa870a0b705b5>
    <"Directory2" ba23af1963e685c6385440ea1d76d92eb0fc4728>
    <"Directory3" eaea4f258ad3488468c52e068994c6289c9ee4ea>
    <"Directory4" 3344b4aa8fec8f28d189d5d2f05285e60f26cbd4>

  2.1.3 :016 > old_thing.diff(new_thing).deltas
   => [#<Rugged::Diff::Delta:70270137419160 {old_file: {:oid=>"edd02898ce2deab5b06f9af79c97225427aa0202", :path=>"Directory1/File1", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory1/File1", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418980 {old_file: {:oid=>"2289c92f857605706b5bb1405e8b62b188777f36", :path=>"Directory1/File2", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory1/File2", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418840 {old_file: {:oid=>"248bce651b8c13085beb99c77b63d83a9866fbe5", :path=>"Directory1/File3", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory1/File3", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418700 {old_file: {:oid=>"252bae4bbacebe7a44e02b688960ccaff0515ee1", :path=>"Directory2/File4", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory2/File4", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418560 {old_file: {:oid=>"a4b3a268c46f5a5340ab8a59fc74ebdc38d9a74a", :path=>"Directory2/File5", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory2/File5", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418420 {old_file: {:oid=>"1928ee6db9fcd32139f8b7bd315766d645aec086", :path=>"Directory3/File6", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory3/File6", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>, 
       #<Rugged::Diff::Delta:70270137418280 {old_file: {:oid=>"d4fc3289375ca9a25b52badad06540fe31f9ed73", :path=>"Directory4/File7", :size=>0, :flags=>4, :mode=>33188}, new_file: {:oid=>"0000000000000000000000000000000000000000", :path=>"Directory4/File7", :size=>0, :flags=>4, :mode=>0}, similarity: 0, status: :deleted>] 

因此,即使我只删除了 3 个文件,增量显示我已经删除了所有内容(7 个文件)。我已经尝试了所有可用的选项diff(),所以我猜我在使用Tree::Builder. 我不允许离开空树对象吗?

4

1 回答 1

2

我怀疑这是因为您将空树插入另一棵树,这不是 git 会做的事情。如果您想删除“MasterDir/Directory1”,那么就这样做吧,它应该可以正常工作。

处理多级树的常用方法是通过Index它负责删除空树。

于 2014-12-06T17:10:13.217 回答