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].index_with { |k| /^\#!.+\s+#{::Regexp.quote(k)}\s*$/ }

Instance Method Summary collapse

Instance Method Details

#hook_file_has_changed?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 18

def hook_file_has_changed?
  on_content_replaced { super }
end

#install_hook_fileObject



22
23
24
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 22

def install_hook_file
  on_content_replaced { super }
end

#on_content_replacedObject



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

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)


39
40
41
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 39

def replace_shebang?
  shebang_replacement.present?
end

#ruby_shebangString

Returns:

  • (String)


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

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)


51
52
53
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 51

def shebang_from
  SHEBANG_PATTERNS.fetch(shebang_replacement)
end

#shebang_replacementSymbol?

Returns:

  • (Symbol, nil)


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

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

  nil
end

#shebang_toString

Returns:

  • (String)


65
66
67
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 65

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

#source_content_with_shebang_replacedString

Returns:

  • (String)


80
81
82
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 80

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

#source_pathString

Returns:

  • (String)


70
71
72
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 70

def source_path
  @temp_source_path || super
end

#source_shebangString

Returns:

  • (String)


75
76
77
# File 'lib/redmine_with_git/patches/redmine_git_hosting/gitolite_hook_patch.rb', line 75

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