Class: Dit

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

Class Method Summary collapse

Class Method Details

.detect_existing_hook(hook) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dit.rb', line 79

def self.detect_existing_hook(hook)
  hook_exists = File.exist?(hook)

  cannot_hook, append_to_hook = false

  if hook_exists
    if `cat #{hook}`.include?('./.git/hooks/dit')
      puts 'Dit hook already installed.'
      cannot_hook = true
    elsif `cat #{hook}`.include?('#!/usr/bin/env bash')
      puts "You have #{hook} hooks already that use bash, so we'll " +
        'append ourselves to the file.'
      append_to_hook = true
    else
      puts "You have #{hook} hooks that use some foreign language, " +
        "so we won't interfere, but we can't hook in there."
      cannot_hook = true
    end
  end

  [append_to_hook, cannot_hook]
end

.hookObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dit.rb', line 33

def self.hook
  Dir.chdir(File.join('.git', 'hooks')) do
    # The following check for the existence of post-commit or post-merge hooks
    # and will not interfere with them if they exist and do not use bash.
    append_to_post_commit, cannot_post_commit = hook 'post-commit'
    append_to_post_merge, cannot_post_merge = hook 'post-merge'

    add_hook('post-commit', append_to_post_commit) unless cannot_post_commit
    add_hook('post-merge', append_to_post_merge) unless cannot_post_merge

    make_dit
    make_ruby_enforcer

    # Make sure they're executable
    FileUtils.chmod '+x', %w(post-commit post-merge dit force-ruby)
  end
end

.initObject

This is the class where all the dit work is done. The thor class is basically a very thin layer on top of this that just calls its methods directly.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dit.rb', line 11

def self.init
  if OS.windows?
    puts 'This is a windows system, and dit does not support windows.'
    puts 'See vulpino/dit issue #1 if you have a potential solution.'
    return
  end

  if Dir.exist?('.git')
    puts 'Dit has detected an existing git repo, and will initialize it to ' +
      'populate your ~ directory with symlinks.'
    puts 'Please confirm this by typing y, or anything else to cancel.'
    response = STDIN.gets.chomp.upcase
    return unless (response == 'Y')
    symlink_all
  else
    Git.init(Dir.getwd)
    puts "Initialized empty Git repository in #{File.join(Dir.getwd, '.git')}"
  end
  hook
  puts 'Dit was successfully hooked into .git/hooks.'
end

.make_ditObject



109
110
111
112
113
114
115
# File 'lib/dit.rb', line 109

def self.make_dit
  File.open('dit', 'a') do |f|
    f.write "#!/usr/bin/env ./.git/hooks/force-ruby\n"
    f.write "require 'dit'\n"
    f.write "Dit.symlink_unlinked\n"
  end
end

.make_ruby_enforcerObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dit.rb', line 117

def self.make_ruby_enforcer
  # The following lines are because git hooks do this weird thing
  # where they prepend /usr/bin to the path and a bunch of other stuff
  # meaning git hooks will use /usr/bin/ruby instead of any ruby
  # from rbenv or rvm or chruby, so we make a script forcing the hook
  # to use our ruby
  ruby_path = `which ruby`
  if ruby_path != '/usr/bin/ruby'
    ruby_folder = File.dirname(ruby_path)
    File.open('force-ruby', 'a') do |f|
      f.write "#!/usr/bin/env bash\n"
      f.write "set -e\n"
      if ENV['RBENV_ROOT']
        # Use Rbenv's shims instead of directly going to ruby bin
        # By the way, if anyone has particular PATHs I should use for
        # RVM or chruby, please let me know!
        f.write "PATH=#{File.join(ENV['RBENV_ROOT'], 'shims')}:$PATH\n"
      else
        f.write "PATH=#{ruby_folder}:$PATH\n"
      end
      f.write "exec ruby \"$@\"\n"
    end
  else
    File.open('force-ruby', 'a') do |f|
      f.write "#!/usr/bin/env bash\n"
      f.write "exec ruby \"$@\"\n"
    end
  end
end

.repoObject



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

def self.repo
  Git.open(Dir.getwd)
end


73
74
75
76
77
# File 'lib/dit.rb', line 73

def self.symlink(a, b)
  File.symlink(a, b)
rescue
  puts "Failed to symlink #{a} to #{b}"
end


64
65
66
67
# File 'lib/dit.rb', line 64

def self.symlink_all
  current_branch = `git rev-parse --abbrev-ref HEAD`.chomp
  symlink_list `git ls-tree -r #{current_branch} --name-only`.split("\n")
end


51
52
53
54
55
56
57
58
# File 'lib/dit.rb', line 51

def self.symlink_list(list)
  list.each do |f|
    f.strip!
    wd_f = File.absolute_path f
    home_f = File.absolute_path(f).gsub(Dir.getwd, Dir.home)
    symlink wd_f, home_f
  end
end


60
61
62
# File 'lib/dit.rb', line 60

def self.symlink_unlinked
  symlink_list `git show --pretty="format:" --name-only HEAD`.split("\n")
end

.write_hook(hook_file, do_append) ⇒ Object



102
103
104
105
106
107
# File 'lib/dit.rb', line 102

def self.write_hook(hook_file, do_append)
  File.open(hook_file, 'a') do |f|
    f.write "#!/usr/bin/env bash\n" unless do_append
    f.write "( exec ./.git/hooks/dit )\n"
  end
end