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

谢谢约翰

4

2 回答 2

4

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

于 2011-04-04T02:05:45.633 回答
0

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

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! ")
于 2012-10-17T12:26:24.203 回答