gpt4 book ai didi

ruby - grit - 尝试将文件添加到 git repo 而不是将它们写入文件系统

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:38 25 4
gpt4 key购买 nike

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

---*----

如果我尝试检查文件,它们就会被删除。关于 hwta 的任何想法我都做错了。

谢谢约翰

最佳答案

我知道这是一篇旧帖子,但我遇到了同样的问题并在寻找解决方案时找到了它。添加文件时,您似乎需要位于 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 block 。希望它也对您有用!

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com