summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer <jacob@gitlab.com>2018-05-15 18:49:43 +0200
committerJacob Vosmaer <jacob@gitlab.com>2018-06-21 18:07:49 +0200
commitc6a0bbde3fb9607c92d2cec709665c0b0e1662d6 (patch)
treef028710e1db67f942e0d3a9d7d8dc5185c8a9e55
parentec88d12bba67f8ee88e8644165eda9ac88bb9af2 (diff)
downloadgitlab-ce-c6a0bbde3fb9607c92d2cec709665c0b0e1662d6.tar.gz
Improve shell code in bin/changelog
-rwxr-xr-xbin/changelog35
1 files changed, 24 insertions, 11 deletions
diff --git a/bin/changelog b/bin/changelog
index 9b60f53ce40..c047522465f 100755
--- a/bin/changelog
+++ b/bin/changelog
@@ -19,7 +19,22 @@ Options = Struct.new(
)
INVALID_TYPE = -1
+module ChangelogHelpers
+ def capture_stdout(cmd)
+ output = IO.popen(cmd, &:read)
+ fail_with "command failed: #{cmd.join(' ')}" unless $?.success?
+ output
+ end
+
+ def fail_with(message)
+ warn "\e[31merror\e[0m #{message}"
+ exit
+ end
+end
+
class ChangelogOptionParser
+ extend ChangelogHelpers
+
Type = Struct.new(:name, :description)
TYPES = [
Type.new('added', 'New feature'),
@@ -108,18 +123,20 @@ 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
+ warn "Invalid category index, please select an index between 1 and #{TYPES.length}"
+ exit
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 +176,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 +216,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 +238,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)