diff options
author | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-06-25 21:32:00 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-06-25 21:32:00 +0000 |
commit | dcdcbfa40b7732b12650d8046a84bed346843c90 (patch) | |
tree | d8193404d133c7554e84ecf0196d3cc9bdb8f7ae /bin | |
parent | 2ee1913f1ab411bf873c9085621cacfaf7502575 (diff) | |
download | gitlab-ce-dcdcbfa40b7732b12650d8046a84bed346843c90.tar.gz |
Improve shelling out in bin/changelog
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/changelog | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/bin/changelog b/bin/changelog index 9b60f53ce40..d7b2a1a2de9 100755 --- a/bin/changelog +++ b/bin/changelog @@ -19,7 +19,24 @@ Options = Struct.new( ) INVALID_TYPE = -1 +module ChangelogHelpers + Abort = Class.new(StandardError) + Done = Class.new(StandardError) + + def capture_stdout(cmd) + output = IO.popen(cmd, &:read) + fail_with "command failed: #{cmd.join(' ')}" unless $?.success? + output + end + + def fail_with(message) + raise Abort, "\e[31merror\e[0m #{message}" + end +end + class ChangelogOptionParser + extend ChangelogHelpers + Type = Struct.new(:name, :description) TYPES = [ Type.new('added', 'New feature'), @@ -68,7 +85,7 @@ class ChangelogOptionParser opts.on('-h', '--help', 'Print help message') do $stdout.puts opts - exit + raise Done.new end end @@ -108,18 +125,19 @@ class ChangelogOptionParser def assert_valid_type!(type) unless type - $stderr.puts "Invalid category index, please select an index between 1 and #{TYPES.length}" - exit 1 + raise Abort, "Invalid category index, please select an index between 1 and #{TYPES.length}" end end def git_user_name - %x{git config user.name}.strip + capture_stdout(%w[git config user.name]).strip end end end class ChangelogEntry + include ChangelogHelpers + attr_reader :options def initialize(options) @@ -159,13 +177,9 @@ class ChangelogEntry end def amend_commit - %x{git add #{file_path}} - exec("git commit --amend") - end + fail_with "git add failed" unless system(*%W[git add #{file_path}]) - def fail_with(message) - $stderr.puts "\e[31merror\e[0m #{message}" - exit 1 + Kernel.exec(*%w[git commit --amend]) end def assert_feature_branch! @@ -203,7 +217,7 @@ class ChangelogEntry end def last_commit_subject - %x{git log --format="%s" -1}.strip + capture_stdout(%w[git log --format=%s -1]).strip end def file_path @@ -225,7 +239,7 @@ class ChangelogEntry end def branch_name - @branch_name ||= %x{git symbolic-ref --short HEAD}.strip + @branch_name ||= capture_stdout(%w[git symbolic-ref --short HEAD]).strip end def remove_trailing_whitespace(yaml_content) @@ -234,8 +248,15 @@ class ChangelogEntry end if $0 == __FILE__ - options = ChangelogOptionParser.parse(ARGV) - ChangelogEntry.new(options) + begin + options = ChangelogOptionParser.parse(ARGV) + ChangelogEntry.new(options) + rescue ChangelogHelpers::Abort => ex + $stderr.puts ex.message + exit 1 + rescue ChangelogHelpers::Done + exit + end end # vim: ft=ruby |