<repo_name>scatterbrain/wave<file_sep>/lib/wave.rb
require 'gitlab_git'
require 'rugged'

module Wave
  extend self

  REPO_PATH='/tmp/example/example.git'
  BLOB_PATH="document.md"

  def gitdo(msg)
    case msg["cmd"]
    when "commit"
      reply = commit(msg, repo())
    when "read"
      reply = read(repo())
    end
  end

  def read(repo)
    begin
      commit = Gitlab::Git::Commit.last(repo)
      blob = Gitlab::Git::Blob.find(repo, commit.sha, BLOB_PATH)
      ok({:doc => blob.data, :author => commit.raw_commit.author, :message => commit.message })
    rescue Exception => ex
      { :result => 1, :error => ex.message, :bt => ex.backtrace.join("\n") }
    end
  end

  def commit(msg, repo)
    #GitLab::Git doesn't have support for commits so we use the raw rugged repo
    repo = repo.rugged
    oid = repo.write(msg["doc"]["text"], :blob)
    index = repo.index
    begin
      index.read_tree(repo.head.target.tree)
    rescue
    end
    index.add(:path => BLOB_PATH, :oid => oid, :mode => 0100644)

    options = {}
    options[:tree] = index.write_tree(repo)

    options[:author] = { :email => "<EMAIL>", :name => 'Mikko', :time => Time.now }
    options[:committer] = { :email => "<EMAIL>", :name => 'Mikko', :time => Time.now }
    options[:message] ||= "It's a commit!"
    options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
    options[:update_ref] = 'HEAD'

    Rugged::Commit.create(repo, options)
    #index.write()
    ok
  end

  def repo()
    begin
      Gitlab::Git::Repository.new(REPO_PATH)
    rescue
      Rugged::Repository.init_at(REPO_PATH, :bare)
      Gitlab::Git::Repository.new(REPO_PATH)      
    end
  end

  def ok(opts = {})
    { :result => 0}.merge(opts)
  end
end
<file_sep>/workers/git_processor.rb
require 'sneakers'
require 'json'
require_relative '../lib/wave'

class GitProcessor
  include Sneakers::Worker
  include Wave
  from_queue "doc.request", 
    env: nil, #Env nil tells not to mangle the name to doc.request_development
    :durable => true

  #from_queue 'downloads',
  #         :durable => false,
  #         :ack => true,
  #         :threads => 50,
  #         :prefetch => 50,
  #         :timeout_job_after => 1,
  #         :exchange => 'dummy',
  #         :heartbeat => 5

  Sneakers.configure({})

  #  Sneakers.configure(:handler => Sneakers::Handlers::Maxretry,
  #                   :workers => 1,
  #                   :threads => 1,
  #                   :prefetch => 1,
  #                   :exchange => 'sneakers',
  #                   :exchange_type => 'topic', ##NOTE WE CAN MAKE A TOPIC
  #                   EXCHANGE!
  #                   :routing_key => ['#', 'something'],
  #                   :durable => true,
  #                   )

  Sneakers.logger.level = Logger::INFO 

  def work_with_params(msg, delivery_info, metadata)
    msg = JSON.parse(msg)    
    reply = gitdo(msg)

    #Reply to delivery_info channel
    exchange = delivery_info.channel.default_exchange
    exchange.publish(reply.to_json, :routing_key => metadata.reply_to, :correlation_id => metadata.correlation_id)

    ack!
  end

end
<file_sep>/Gemfile
source 'https://rubygems.org'
gem 'sneakers', '~> 1.0.1'
gem 'gitlab_git', '~> 7.0.0.rc12'
