Module: GitIssue::Helper

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

Constant Summary collapse

CONFIGURE_MESSAGE =
<<-END
please set issue tracker %s.

  %s
END

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_error(attr_name, example) ⇒ Object



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

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

.configured_value(name, trim = true) ⇒ Object



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

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

.get_body_from_editor(message = nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/git_issue.rb', line 113

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



105
106
107
108
109
110
111
# File 'lib/git_issue.rb', line 105

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



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

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

.its_klass_of(its_type) ⇒ Object



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

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

Instance Method Details

#git_editorObject



70
71
72
73
74
75
76
# File 'lib/git_issue.rb', line 70

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.strip| ... } ⇒ Object

Yields:

  • (text.strip)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/git_issue.rb', line 120

def open_editor(message = nil, abort_if_not_modified = true , &block)
  message_file = File.join(work_dir, 'ISSUE_MESSAGE')
  File.open(message_file, 'w') { |msg|
    msg.puts message
  }
  begin
    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
  ensure
    File.unlink(message_file)
  end

  yield text.strip
end

#read_body(file) ⇒ Object



98
99
100
101
102
103
# File 'lib/git_issue.rb', line 98

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

#split_head_and_body(text) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git_issue.rb', line 85

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

#work_dirObject



78
79
80
81
82
83
# File 'lib/git_issue.rb', line 78

def work_dir
  dir = RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|bccwin|cygwin/ ?
    `git rev-parse -q --git-dir 2> NUL`.strip :
    `git rev-parse -q --git-dir 2> /dev/null`.strip
  dir.empty? ? Dir.tmpdir : dir
end