Module: RedmineWithGit::Patches::RedmineGitHosting::GitoliteHookPatch::InstanceMethods

Defined in:
lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb

Constant Summary collapse

SHEBANG_PATTERNS =
[:ruby].map { |k| [k, /^\#!.+\s+#{::Regexp.quote(k)}\s*$/] }
.to_h

Instance Method Summary collapse

Instance Method Details

#hook_file_has_changed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 20

def hook_file_has_changed?
  on_content_replaced { super }
end

#install_hook_fileObject



24
25
26
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 24

def install_hook_file
  on_content_replaced { super }
end

#on_content_replacedObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 28

def on_content_replaced
  return yield unless replace_shebang?

  ::EacRubyUtils::Fs::Temp.on_file do |temp_path|
    temp_path.write(source_content_with_shebang_replaced)
    @temp_source_path = temp_path.to_path
    yield
  ensure
    @temp_source_path = nil
  end
end

#replace_shebang?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 41

def replace_shebang?
  shebang_replacement.present?
end

#ruby_shebangString

Returns:

  • (String)


46
47
48
49
50
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 46

def ruby_shebang
  bin = ::RbConfig::CONFIG['RUBY_INSTALL_NAME'] || ::RbConfig::CONFIG['ruby_install_name']
  bin += (::RbConfig::CONFIG['EXEEXT'] || ::RbConfig::CONFIG['exeext'] || '')
  ::File.join(::RbConfig::CONFIG['bindir'], bin)
end

#shebang_fromRegexp

Returns:

  • (Regexp)


53
54
55
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 53

def shebang_from
  SHEBANG_PATTERNS.fetch(shebang_replacement)
end

#shebang_replacementSymbol?

Returns:

  • (Symbol, nil)


58
59
60
61
62
63
64
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 58

def shebang_replacement
  SHEBANG_PATTERNS.each do |k, v|
    return k if v.match?(source_shebang)
  end

  nil
end

#shebang_toString

Returns:

  • (String)


67
68
69
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 67

def shebang_to
  "\#\!#{send("#{shebang_replacement}_shebang")}\n"
end

#source_content_with_shebang_replacedString

Returns:

  • (String)


82
83
84
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 82

def source_content_with_shebang_replaced
  ::File.read(source_path).gsub(shebang_from, shebang_to)
end

#source_pathString

Returns:

  • (String)


72
73
74
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 72

def source_path
  @temp_source_path || super
end

#source_shebangString

Returns:

  • (String)


77
78
79
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 77

def source_shebang
  ::File.open(source_path, &:readline).strip
end