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

发布于 2024-09-04 06:51:07 字数 1291 浏览 4 评论 0原文

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 的任何想法我都做错了。

谢谢 约翰

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

---*----

If I try to check out the files they get deleted. Any ideas on hwta I am doing wrong.

Thanks
John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

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

I know this is an old post, but I was having the same problem and found it when searching for a solution. Looks like you need to be in the repo directory when adding the file(s). Here's what I did to make it work...

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!')

The key step is the Dir.chdir block. Hopefully it will work for you too!

↘紸啶 2024-09-11 06:51:07

在我看来,毅力并没有真正遵循POLA,所以这就是为什么如何添加所有更改的文件。我的学习曲线是,如果传递给 repo.add,Grit 返回的文件(例如 repo.status.files)将不起作用。相反,您需要使用文件名。我需要进入源代码来解决这个问题。

location = '/foo/bar/my_repo'
repo = Grit::Repo.new(location)
# modified, added, un-tracked files
changed_files = repo.status.files.select { |k,v| (v.type =~ /(M|A)/ || v.untracked) }

Dir.chdir(location) {
  changed_files.each do |changed_file|
    # changed_file here is array, which first element is name of file 
    # e.g. changed_file.first => "example.txt."
    repo.add(changed_file.first)
  end
}

repo.commit_index("Successful commit! yeeeee! ")

In my opinion grit is not really following POLA so here is why how to add all changed files. My learning curve here was that Grit returned files (e.g. repo.status.files ) will not work if passed to repo.add. Instead you need to use file names. I needed to go into source code to figure this out.

location = '/foo/bar/my_repo'
repo = Grit::Repo.new(location)
# modified, added, un-tracked files
changed_files = repo.status.files.select { |k,v| (v.type =~ /(M|A)/ || v.untracked) }

Dir.chdir(location) {
  changed_files.each do |changed_file|
    # changed_file here is array, which first element is name of file 
    # e.g. changed_file.first => "example.txt."
    repo.add(changed_file.first)
  end
}

repo.commit_index("Successful commit! yeeeee! ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文