开发者

Determine which git files have changed using Ruby?

I've got a Rails app with blog entries that I want to update from a separate Blog.git repo.

I envision my workflow as something like:

  1. Write new blog entry
  2. Push to remote Git repo
  3. Call cap deploy:update, which will invoke a Rake task to update the database with any changed or new entries

The hitch here is finding which files have changed. I'd like to harness Git for this, and I know I could do some awk or Perl scripting on git diff.

But is there a better way? I've briefly looked at Grit but can't find a good solution.

Update: It turns out Grit is the best solution to this problem, at least as far as I can tell. Here's what I used to solve the problem:

desc 'Posts all entries to database'
task :post_all do
  Dir.chdir REPO do
    Grit::Repo.new('.').tree.contents.each do |file|
      # post_entry cleans up my blog entries and posts them via Post.create()
      post_entry(file.data, :text) unless file.basename =~ /\.gitignore/
    end
  end
end

desc 'Posts all new or changed entries to database'
task :post_new do
  Dir.chdir REPO do
    Grit::Repo.new('.').head.commit.diffs.each do |diff|
      post_entry diff.b_blob.data, :text
    end
  end
end

desc 'Deletes entries from database'
task :remove_all do
  Post.destroy_all
end

desc 'Synchronizes the remote blog repo and开发者_C百科 the database'
task :sync => [ :remove_all, :post_all ]


is a database really necessary? check out the jekyll gem. hundreds (if not thousands) of simple blogs use it, including two of mine.

http://github.com/mojombo/jekyll

otherwise, grit is a good solution. i've used it for a few things and it works well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜