Class: Hookup

Inherits:
Object
  • Object
show all
Defined in:
lib/hookup.rb

Defined Under Namespace

Classes: Error, Failure, PostCheckout

Constant Summary collapse

EMPTY_DIR =
'4b825dc642cb6eb9a060e54bf8d69288fbee4904'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(*argv) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/hookup.rb', line 11

def self.run(*argv)
  new.run(*argv)
rescue Failure => e
  puts e
  exit 1
rescue Error => e
  puts e
  exit
end

Instance Method Details

#git_dirObject



36
37
38
39
40
41
42
# File 'lib/hookup.rb', line 36

def git_dir
  unless @git_dir
    @git_dir = %x{git rev-parse --git-dir}.chomp
    raise Error, dir unless $?.success?
  end
  @git_dir
end

#installObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hookup.rb', line 44

def install
  append(File.join(git_dir, 'hooks', 'post-checkout'), 0777) do |body, f|
    f.puts "#!/bin/bash" unless body
    f.puts %(hookup post-checkout "$@") if body !~ /hookup/
  end

  append(File.join(git_dir, 'info', 'attributes')) do |body, f|
    map = 'db/schema.rb merge=railsschema'
    f.puts map unless body.to_s.include?(map)
  end

  system 'git', 'config', 'merge.railsschema.driver', 'hookup resolve-schema %A %O %B %L'

  puts "Hooked up!"
end

#post_checkout(*args) ⇒ Object



69
70
71
# File 'lib/hookup.rb', line 69

def post_checkout(*args)
  PostCheckout.new(ENV, *args).run
end

#resolve_schema(a, o, b, marker_size = 7) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/hookup.rb', line 207

def resolve_schema(a, o, b, marker_size = 7)
  system 'git', 'merge-file', "--marker-size=#{marker_size}", a, o, b
  body = File.read(a)
  asd = "ActiveRecord::Schema.define"
  x = body.sub!(/^<+ .*\n#{asd}\(:version => (\d+)\) do\n=+\n#{asd}\(:version => (\d+)\) do\n>+ .*/) do
    "#{asd}(:version => #{[$1, $2].max}) do"
  end
  File.open(a, 'w') { |f| f.write(body) }
  if body.include?('<' * marker_size.to_i)
    raise Failure, 'Failed to automatically resolve schema conflict'
  end
end

#run(*argv) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hookup.rb', line 21

def run(*argv)
  if argv.empty?
    install
  else
    command = argv.shift
    begin
      send(command.tr('-', '_'), *argv)
    rescue NoMethodError
      raise Error, "Unknown command #{command}"
    rescue ArgumentError
      raise Error, "Invalid arguments for #{command}"
    end
  end
end