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に従っていないため、変更されたすべてのファイルを追加する方法は次のとおりです。ここでの私の学習曲線は、Grit が返したファイル (例: repo.status.files) が に渡された場合に機能しないということでした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 に答える