简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM