2010-06-05 53 views
1
mkdir /tmp/scratch 
cd /tmp/scratch 
git init . 

- * - xx.rb:砂礫 - 嘗試添加文件到用的git回購了其寫入到文件系統

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}", "[email protected]#{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

我知道這是一個古老的職位,但我有同樣的問題,發現它的解決方案搜索時。看起來您在添加文件時需要在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塊。希望它也能爲你工作!

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