Class: Dit

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

Class Method Summary collapse

Class Method Details

.hookObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dit.rb', line 29

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, append_to_post_merge, cannot_post_commit, 
      cannot_post_merge = detect_existing_hooks

    unless cannot_post_commit
      File.open("post-commit", "a") do |f|
        f.write "#!/usr/bin/env bash\n" unless append_to_post_commit
        f.write "( exec ./.git/hooks/dit )\n"
      end
    end

    unless cannot_post_merge
      File.open("post-merge", "a") do |f|
        f.write "#!/usr/bin/env bash\n" unless append_to_post_merge
        f.write "( exec ./.git/hooks/dit )\n"
      end
    end

    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
      
    # 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
    # Make sure they're executable
    FileUtils.chmod '+x', %w(post-commit post-merge dit force-ruby)
  end
end

.initObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dit.rb', line 8

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."
    return unless gets.chomp! === '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


100
101
102
103
# File 'lib/dit.rb', line 100

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


88
89
90
91
92
93
94
# File 'lib/dit.rb', line 88

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


96
97
98
# File 'lib/dit.rb', line 96

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