new plugin: acts_as_git

Courtenay : November 14th, 2008

With the help of Jamie van Dyke at Parfait and Scott Chacon at GitHub, I'm pleased to announce Acts As Git (no, I don't like the name either). It's a simple plugin which stores all changes you make to a text field in a git repository. This is ideal for something like a git-backed wiki.

Look at it here: github or check it out from

git://github.com/courtenay/acts_like_git.git

From the README:

ALG automagically saves the history of a given text or string field. It sits over the top of an ActiveRecord model; after a value is committed to the database, the plugin writes the new value to a text file and commits it to a git repository. This way you get all the advantages of using Git as version-control.

Usage:

class Post < ActiveRecord::Base
  versioning(:title) do |version|
    version.repository = '/home/git/repositories/postal.git'
    version.message = lambda { |post| "Committed by #{post.author.name}" }
  end
end

To view the complete list of changes:

>> @post = Post.find 15
<Post:15>
>> @post.title
=> 'Freddy'
>> @post.history(:title)
=> ['Joe', 'Frank', 'Freddy]
>> @post.log
=> ['bfec2f69e270d2d02de4e8c7a4eb2bd0f132bdbb', '643deb45c12982dde75ba71657792a2dbdda83e6', 
'1ce6c7368219db7698f4acc3417e656510b4138d']
>> @post.revert_to '1ce6c7368219db7698f4acc3417e656510b4138d'
>> @post.title
=> 'Joe'

It uses the excellent Grit library, and doesn't actually have a checked-out repository. The latest version of your data is still stored in the database. You can actually clone this repo and view the changes; pushing back to it won't do anything useful.

8 Responses to “new plugin: acts_as_git”

  1. Jeremy Kemper Says:

    Sweet plugin. Thanks for sharing it!

  2. Justin Blake Says:

    This is the coolest thing I have ever seen.

  3. Scott Says:

    Very nice. I have just the right project for this.

  4. Pratik Says:

    That's brilliant !!!

  5. Trevor Turk Says:

    This looks really interesting! I don't understand what that last sentence means, though. Are you saying that changes made to the git repo directly won't be reflected in the app?

  6. Anil Wadghule Says:

    Awesome

  7. Justin Says:

    Doesn't ikiwiki do this? (the revision-control-system-backed wiki idea, at least)

  8. hachiya Says:

    I was using actslikegit and found a problem.

    Bascially:

    p1=Post.find(1)
    p2=Post.find(2)
    p1.body="new value for post1"
    p1.save
    # p2.body will ALSO now show "new value for post1"
    # p2['body'] still has the original value (correct)
    

    This bug doesn't show up if you reload the app or script/console, as it is a cached value. p2.body is incorrect, but p2['body'] still has the correct value.

    This is because p1 and p2 share the variable, @versionedfieldsvalues as a class variable.

    It seems this should be an instance variable instead.

    A patched fork is available at http://github.com/hachiya/actslikegit

    I've submitted a pull request to you guys. Feel free to use as desired.

    Hopefully this helps someone.

Sorry, comments are closed for this article.