Module: GitIssue::Helper

Included in:
Base
Defined in:
lib/git_issue.rb

Constant Summary collapse

CONFIGURE_MESSAGE =
"please set issue tracker %s.\n\n  %s\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_error(attr_name, example) ⇒ Object



43
44
45
# File 'lib/git_issue.rb', line 43

def configure_error(attr_name, example)
  raise CONFIGURE_MESSAGE % [attr_name, example]
end

.configured_value(name, trim = true) ⇒ Object



48
49
50
51
52
# File 'lib/git_issue.rb', line 48

def configured_value(name, trim = true)
  res = `git config #{name}`
  res = trim ? res.strip : res
  res
end

.get_body_from_editor(message = nil) ⇒ Object



108
109
110
111
112
113
# File 'lib/git_issue.rb', line 108

def get_body_from_editor(message=nil)
  open_editor(message) do |text|
    abort "Aborting due to empty message" if text.empty?
    text
  end
end

.get_title_and_body_from_editor(message = nil) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/git_issue.rb', line 100

def get_title_and_body_from_editor(message=nil)
  open_editor(message) do |text|
    title, body = split_head_and_body(text)
    abort "Aborting due to empty issue title" unless title
    [title, body]
  end
end

.global_configured_value(name) ⇒ Object



54
55
56
57
# File 'lib/git_issue.rb', line 54

def global_configured_value(name)
  res = `git config --global #{name}`
  res.strip
end

.its_klass_of(its_type) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/git_issue.rb', line 59

def its_klass_of(its_type)
  case its_type
    when /redmine/i then GitIssue::Redmine
    when /github/i  then GitIssue::Github
    else
      raise "unknown issue tracker type : #{its_type}"
  end
end

Instance Method Details

#git_dirObject



76
77
78
# File 'lib/git_issue.rb', line 76

def git_dir
  `git rev-parse -q --git-dir`.strip
end

#git_editorObject



68
69
70
71
72
73
74
# File 'lib/git_issue.rb', line 68

def git_editor
  # possible: ~/bin/vi, $SOME_ENVIRONMENT_VARIABLE, "C:\Program Files\Vim\gvim.exe" --nofork
  editor = `git var GIT_EDITOR`
  editor = ENV[$1] if editor =~ /^\$(\w+)$/
  editor = File.expand_path editor if (editor =~ /^[~.]/ or editor.index('/')) and editor !~ /["']/
  editor.shellsplit
end

#open_editor(message = nil, abort_if_not_modified = true) {|text| ... } ⇒ Object

Yields:

  • (text)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/git_issue.rb', line 115

def open_editor(message = nil, abort_if_not_modified = true , &block)
  message_file = File.join(git_dir, 'ISSUE_MESSAGE')
  File.open(message_file, 'w') { |msg|
    msg.puts message
  }
  edit_cmd = Array(git_editor).dup
  edit_cmd << '-c' << 'set ft=gitcommit' if edit_cmd[0] =~ /^[mg]?vim$/
  edit_cmd << message_file

  system(*edit_cmd)
  abort "can't open text editor for issue message" unless $?.success?

  text = read_body(message_file)
  abort "Aborting cause messages didn't modified." if message == text && abort_if_not_modified

  yield text
end

#read_body(file) ⇒ Object



93
94
95
96
97
98
# File 'lib/git_issue.rb', line 93

def read_body(file)
  f = open(file)
  body = f.read
  f.close
  body.strip
end

#split_head_and_body(text) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/git_issue.rb', line 80

def split_head_and_body(text)
  title, body = '', ''
  text.each_line do |line|
    next if line.index('#') == 0
    ((body.empty? and line =~ /\S/) ? title : body) << line
  end
  title.tr!("\n", ' ')
  title.strip!
  body.strip!

  [title =~ /\S/ ? title : nil, body =~ /\S/ ? body : nil]
end