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:
- Write new blog entry
- Push to remote Git repo
- 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 ]