mkdir /tmp/scratch
cd /tmp/scratch
git init .

--*--
XX.RB公司:
SCRATCH = '/tmp/scratch'
repo = Repo.new(SCRATCH)

def add_multiple_commits_same_file_different_content(repo)
  previous_commit = repo.commits.first && repo.commits.first.id
  dir = "./"
  (0...5).each do |count|
    i1 = repo.index
    i1.read_tree('master')
    i1.add("#{dir}foo.txt", "hello foo, count is #{count}.\n")
    dir += "sd#{count}/"
    previous_commit =  i1.commit("my commit - #{count}",
                             previous_commit,
                             Actor.new("j#{count}", "e@e#{count}.zz"),
                             previous_commit.nil? ? nil : repo.commits(previous_commit).first.tree)
  end
end

add_multiple_commits_same_file_different_content(repo)

---* ---
git status:
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    ./foo.txt
#       deleted:    ./sd0/foo.txt
#       deleted:    ./sd0/sd1/foo.txt
#       deleted:    ./sd0/sd1/sd2/foo.txt
#       deleted:    ./sd0/sd1/sd2/sd3/foo.txt
#       deleted:    ./sd0/sd1/sd2/sd3/sd4/foo.txt

---*----
如果我试着去查那些被删除的文件任何关于我做错事的想法。
谢谢
约翰

最佳答案

我知道这是一篇老文章,但我也遇到了同样的问题,在寻找解决方案时发现了它。添加文件时,似乎需要在repo目录中我是这么做的。。。

repo = Grit::Repo.init('repo/test.git')

File.open('repo/test.git/foo.txt', 'w') { |f| f.puts 'Hello World!' }

Dir.chdir('repo/test.git') { repo.add('foo.txt') }

repo.commit_index('This commit worked!')

关键步骤是dir.chdir块。希望对你也有用!

关于ruby - grit-尝试将文件添加到git repo中,而无需将其写入文件系统,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2978781/

10-14 23:00
查看更多