简体   繁体   English

确定使用Ruby更改了哪些git文件?

[英]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. 我有一个带有博客条目的Rails应用程序,我想从一个单独的Blog.git存储库中进行更新。

I envision my workflow as something like: 我将工作流程设想为:

  1. Write new blog entry 撰写新博客条目
  2. Push to remote Git repo 推送到远程Git仓库
  3. Call cap deploy:update, which will invoke a Rake task to update the database with any changed or new entries 调用上限deploy:update,它将调用Rake任务以使用任何已更改或新条目更新数据库

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 . 我想利用Git做到这一点,而且我知道我可以在git diff上做一些awk或Perl脚本。

But is there a better way? 但有更好的方法吗? I've briefly looked at Grit but can't find a good solution. 我简要看过Grit,但找不到一个好的解决方案。

Update : It turns out Grit is the best solution to this problem, at least as far as I can tell. 更新 :事实证明,Grit是解决此问题的最佳方法,至少就我所知。 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. 查看jekyll宝石。 hundreds (if not thousands) of simple blogs use it, including two of mine. 数百个(甚至数千个)简单博客都在使用它,其中包括两个。

http://github.com/mojombo/jekyll http://github.com/mojombo/jekyll

otherwise, grit is a good solution. 否则,砂砾是一个很好的解决方案。 i've used it for a few things and it works well. 我已经将它用于一些事情,并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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