1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby

# Erik Kastner - git-r-up.rb
# daemon to watch for changes in a directory and sync them to a directory under source control (like rails + git)
# also adds and commits files to git (just git init in the FETCH_ROOT)

require 'rubygems'
require 'simple-daemon'
require 'git'
require 'syslog'

PUBLIC_ROOT = "/u/apps/app/current/public"
FETCH_ROOT = "/home/app/website"

def log(msg); Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.debug msg }; end

class Watcher < SimpleDaemon::Base
  SimpleDaemon::WORKING_DIRECTORY = "/tmp/git-watcher"
  def self.start
    log "Starting Watcher"
    g = Git.open(FETCH_ROOT)
    loop do
      unless g.status.untracked.empty? && g.status.changed.empty?
        log "changed files"
        begin
          %x{cp -r #{FETCH_ROOT}/* #{PUBLIC_ROOT}}
          g.add
          g.commit("auto from watcher")
        rescue => e
          log e.inspect
        end
        sleep(7) # give git time to do it's thing
      end
      sleep(2)
    end
  end
  
  def self.end
    puts "Stopping Watcher"
  end
end

Watcher.daemonize