gitflash 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -0
- data/README.md +63 -4
- data/bin/gitflash +2 -1
- data/command_descriptions.yml +36 -21
- data/lib/gitflash/branch.rb +44 -0
- data/lib/gitflash/cli.rb +50 -86
- data/lib/gitflash/commands/base.rb +38 -0
- data/lib/gitflash/commands/branches.rb +68 -0
- data/lib/gitflash/commands/checkout.rb +47 -0
- data/lib/gitflash/commands/delete.rb +106 -0
- data/lib/gitflash/commands/reset.rb +67 -0
- data/lib/gitflash/commit.rb +22 -0
- data/lib/gitflash/configuration.rb +3 -1
- data/lib/gitflash/confirmation_required.rb +19 -0
- data/lib/gitflash/error.rb +23 -0
- data/lib/gitflash/git/bash_command.rb +15 -4
- data/lib/gitflash/git/command_error.rb +11 -0
- data/lib/gitflash/prompt.rb +2 -2
- data/lib/gitflash/repo.rb +116 -0
- data/lib/gitflash/result.rb +8 -0
- data/lib/gitflash/ui.rb +93 -0
- data/lib/gitflash/usage_error.rb +14 -0
- data/lib/gitflash/version.rb +1 -1
- data/schema/v1.json +172 -0
- metadata +30 -29
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/lib/gitflash/git/wrapper.rb +0 -80
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
module Commands
|
|
5
|
+
# Deletes local branches given as arguments or picked from a menu.
|
|
6
|
+
# Uses `git branch -d` (merged branches only) unless --force is given.
|
|
7
|
+
# The result lists each deleted branch with its last commit, so it can be recreated.
|
|
8
|
+
class Delete < Base
|
|
9
|
+
PROTECTED_NAMES = %w[main master].freeze
|
|
10
|
+
|
|
11
|
+
def call(*names)
|
|
12
|
+
branches = repo.branches
|
|
13
|
+
candidates = branches.reject { |branch| protected?(branch) }.map(&:name)
|
|
14
|
+
|
|
15
|
+
names = pick(candidates) if names.empty?
|
|
16
|
+
return 0 if names.nil?
|
|
17
|
+
|
|
18
|
+
validate!(names.uniq, branches)
|
|
19
|
+
delete(names.uniq, branches.to_h { |branch| [branch.name, branch.sha] })
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
# Branches picked from the menu, or nil after telling the user there is nothing to delete
|
|
25
|
+
def pick(candidates)
|
|
26
|
+
require_interactive!('Pass the branches to delete: gitflash delete BRANCH...')
|
|
27
|
+
return report_nothing('No branches available to delete') if candidates.empty?
|
|
28
|
+
|
|
29
|
+
names = ui.multi_select('Select branches to delete', candidates)
|
|
30
|
+
names.empty? ? report_nothing('No branches selected') : names
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def force?
|
|
34
|
+
options[:force] ? true : false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def protected?(branch)
|
|
38
|
+
branch.current? || branch.default? || PROTECTED_NAMES.include?(branch.name)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def validate!(names, branches)
|
|
42
|
+
by_name = branches.to_h { |branch| [branch.name, branch] }
|
|
43
|
+
unknown = names.reject { |name| by_name.key?(name) }
|
|
44
|
+
usage_error!('unknown_branch', "Unknown branch: #{unknown.join(', ')}") if unknown.any?
|
|
45
|
+
|
|
46
|
+
refused = names.select { |name| protected?(by_name[name]) }
|
|
47
|
+
return if refused.empty?
|
|
48
|
+
|
|
49
|
+
usage_error!('protected_branch',
|
|
50
|
+
"Refusing to delete protected branches: #{refused.join(', ')} " \
|
|
51
|
+
'(current, default, main and master are protected)')
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def delete(names, shas)
|
|
55
|
+
plan = { branches: names, force: force? }
|
|
56
|
+
return planned(plan, "Would delete:\n#{bullets(names)}") if ui.dry_run?
|
|
57
|
+
return cancelled(plan) unless ui.confirm?(summary(names), plan: plan)
|
|
58
|
+
|
|
59
|
+
outcomes = names.map { |name| [name, repo.delete_branch(name, force: force?)] }
|
|
60
|
+
report_results(plan, outcomes, shas)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def summary(names)
|
|
64
|
+
mode = force? ? 'even if they have unmerged changes' : 'if they are fully merged'
|
|
65
|
+
"You are about to delete the following branches #{mode}:\n\n#{bullets(names)}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def bullets(names)
|
|
69
|
+
names.map { |name| "* #{name}" }.join("\n")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def report_nothing(text)
|
|
73
|
+
ui.report(status: 'noop', result: { deleted: [], failed: [] }, text: text)
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def report_results(plan, outcomes, shas)
|
|
78
|
+
deleted, failed = outcomes.partition { |_name, result| result.success? }
|
|
79
|
+
result = {
|
|
80
|
+
deleted: deleted.map { |name, _result| { branch: name, sha: shas[name] } },
|
|
81
|
+
failed: failed.map { |name, result| { branch: name, error: result.output } }
|
|
82
|
+
}
|
|
83
|
+
ui.report(status: failed.empty? ? 'done' : 'failed', plan: plan, result: result,
|
|
84
|
+
error: failure(failed, outcomes), text: results_text(result))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def failure(failed, outcomes)
|
|
88
|
+
return nil if failed.empty?
|
|
89
|
+
|
|
90
|
+
message = "#{failed.size} of #{outcomes.size} branches were not deleted"
|
|
91
|
+
{ code: 'git_failed', message: message, exit_code: 1 }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def force_hint?(result)
|
|
95
|
+
result[:failed].any? && !force?
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def results_text(result)
|
|
99
|
+
lines = result[:deleted].map { |row| "Deleted branch #{row[:branch]} (was #{row[:sha]})" }
|
|
100
|
+
lines += result[:failed].map { |row| "Not deleted #{row[:branch]}: #{row[:error]}" }
|
|
101
|
+
lines << 'Use --force to delete branches with unmerged changes.' if force_hint?(result)
|
|
102
|
+
lines.join("\n")
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
module Commands
|
|
5
|
+
# Resets the current branch to a commit given as an argument or picked from a menu.
|
|
6
|
+
# Default mode is mixed; --soft and --hard are supported. Only --hard asks for confirmation.
|
|
7
|
+
# The result includes the previous HEAD, so the reset can be reverted.
|
|
8
|
+
class Reset < Base
|
|
9
|
+
HARD_RESET_WARNING = 'You are about to reset your branch and lose all your current changes'
|
|
10
|
+
|
|
11
|
+
def call(ref = nil)
|
|
12
|
+
mode = reset_mode
|
|
13
|
+
|
|
14
|
+
if ref.nil?
|
|
15
|
+
require_interactive!('Pass the commit to reset to: gitflash reset COMMIT')
|
|
16
|
+
commits = repo.commits
|
|
17
|
+
return report_single_commit if commits.size < 2
|
|
18
|
+
|
|
19
|
+
ref = ui.select('Select a commit to reset to', commits.to_h { |c| [c.label, c.sha] })
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
reset(ref, mode)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def reset_mode
|
|
28
|
+
if options[:soft] && options[:hard]
|
|
29
|
+
usage_error!('invalid_options', 'Use either --soft or --hard, not both')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
return 'hard' if options[:hard]
|
|
33
|
+
|
|
34
|
+
options[:soft] ? 'soft' : 'mixed'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def reset(ref, mode)
|
|
38
|
+
commit = repo.resolve_commit(ref)
|
|
39
|
+
usage_error!('unknown_commit', "Unknown commit '#{ref}'") if commit.nil?
|
|
40
|
+
|
|
41
|
+
plan = { commit: commit, mode: mode }
|
|
42
|
+
text = "reset to #{commit[0, 7]} (#{mode})"
|
|
43
|
+
return planned(plan, "Would #{text}") if ui.dry_run?
|
|
44
|
+
return cancelled(plan) unless confirmed?(plan)
|
|
45
|
+
|
|
46
|
+
execute(plan, text)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def execute(plan, text)
|
|
50
|
+
previous = repo.resolve_commit('HEAD')
|
|
51
|
+
result = repo.reset(plan[:commit], mode: plan[:mode])
|
|
52
|
+
git_error!('reset', result) unless result.success?
|
|
53
|
+
|
|
54
|
+
ui.report(status: 'done', plan: plan, result: plan.merge(previous_commit: previous),
|
|
55
|
+
text: text.capitalize)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def confirmed?(plan)
|
|
59
|
+
plan[:mode] != 'hard' || ui.confirm?(HARD_RESET_WARNING, plan: plan)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def report_single_commit
|
|
63
|
+
ui.report(status: 'noop', result: nil, text: 'You only have one commit!')
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
module Gitflash
|
|
6
|
+
# A commit as reported by `git log`
|
|
7
|
+
Commit = Data.define(:sha, :subject, :author, :committed_at) do
|
|
8
|
+
# Parses one line produced with Repo::COMMIT_FORMAT
|
|
9
|
+
def self.parse(line)
|
|
10
|
+
sha, subject, author, date = line.split(Repo::SEPARATOR, 4)
|
|
11
|
+
new(sha: sha, subject: subject.to_s, author: author, committed_at: Time.iso8601(date))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def label
|
|
15
|
+
"#{sha} - #{subject}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_h
|
|
19
|
+
super.merge(committed_at: committed_at.iso8601)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'yaml'
|
|
2
4
|
|
|
3
5
|
module Gitflash
|
|
@@ -20,7 +22,7 @@ module Gitflash
|
|
|
20
22
|
keys = data.keys.map(&:to_sym)
|
|
21
23
|
Struct.new(*keys).new(*data.values).tap do |st|
|
|
22
24
|
keys.each do |method|
|
|
23
|
-
st.
|
|
25
|
+
st.public_send("#{method}=", transform_to_struct(st.public_send(method)))
|
|
24
26
|
end
|
|
25
27
|
end
|
|
26
28
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Raised when a destructive command runs without a terminal and without --yes.
|
|
5
|
+
# Carries the plan so the caller can review it and re-run with --yes.
|
|
6
|
+
class ConfirmationRequired < Error
|
|
7
|
+
def initialize(message = nil, plan: nil)
|
|
8
|
+
super(message, code: 'confirmation_required', plan: plan)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def exit_code
|
|
12
|
+
2
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def status
|
|
16
|
+
'confirmation_required'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Base class for errors reported to the user.
|
|
5
|
+
# `code` is a stable machine-readable identifier; `plan` is the planned change, if any.
|
|
6
|
+
class Error < StandardError
|
|
7
|
+
attr_reader :code, :plan
|
|
8
|
+
|
|
9
|
+
def initialize(message = nil, code: 'failed', plan: nil)
|
|
10
|
+
super(message)
|
|
11
|
+
@code = code
|
|
12
|
+
@plan = plan
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def exit_code
|
|
16
|
+
1
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def status
|
|
20
|
+
'error'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'open3'
|
|
4
|
+
|
|
3
5
|
module Gitflash
|
|
4
6
|
module Git
|
|
5
7
|
class BashCommand
|
|
6
8
|
class << self
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
# Runs a command without a shell and returns its stdout.
|
|
10
|
+
# Raises CommandError when the command exits with a non-zero status.
|
|
11
|
+
def exec(*args)
|
|
12
|
+
stdout, stderr, success = capture(*args)
|
|
13
|
+
raise CommandError, "#{args.join(' ')} failed: #{stderr.strip}" unless success
|
|
14
|
+
|
|
15
|
+
stdout
|
|
9
16
|
end
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
# Runs a command without a shell and returns [stdout, stderr, success].
|
|
19
|
+
def capture(*args)
|
|
20
|
+
stdout, stderr, status = Open3.capture3(*args)
|
|
21
|
+
[stdout, stderr, status.success?]
|
|
22
|
+
rescue SystemCallError => e
|
|
23
|
+
raise CommandError, "#{args.join(' ')} failed: #{e.message}"
|
|
13
24
|
end
|
|
14
25
|
end
|
|
15
26
|
end
|
data/lib/gitflash/prompt.rb
CHANGED
|
@@ -23,12 +23,12 @@ module Gitflash
|
|
|
23
23
|
super(message, collection, default_select_options(options))
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def proceed_with_warning(message
|
|
26
|
+
def proceed_with_warning(message)
|
|
27
27
|
raise ArgumentError, 'A block is required for this method' unless block_given?
|
|
28
28
|
|
|
29
29
|
warn(message)
|
|
30
30
|
answer = yes?('Do you want to proceed?')
|
|
31
|
-
answer ?
|
|
31
|
+
answer ? yield : 'Exited'
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
private
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Reads and changes the git repository in the current working directory.
|
|
5
|
+
# All git access goes through this class; it never prompts or prints.
|
|
6
|
+
class Repo
|
|
7
|
+
SEPARATOR = "\x1f"
|
|
8
|
+
BRANCH_FORMAT = %w[
|
|
9
|
+
refname:short objectname:short HEAD upstream:short upstream:track,nobracket
|
|
10
|
+
committerdate:iso-strict authorname subject
|
|
11
|
+
].map { |field| "%(#{field})" }.join('%1f')
|
|
12
|
+
COMMIT_FORMAT = %w[%h %s %an %cI].join('%x1f')
|
|
13
|
+
COMMITS_LIMIT = 100
|
|
14
|
+
FALLBACK_DEFAULT_BRANCHES = %w[main master].freeze
|
|
15
|
+
|
|
16
|
+
def initialize(bash: Git::BashCommand)
|
|
17
|
+
@bash = bash
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def ensure_work_tree!
|
|
21
|
+
inside = @bash.exec('git', 'rev-parse', '--is-inside-work-tree').strip == 'true'
|
|
22
|
+
raise Error.new('Not a git repository', code: 'not_a_repository') unless inside
|
|
23
|
+
rescue Git::CommandError
|
|
24
|
+
raise Error.new('Not a git repository', code: 'not_a_repository')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Local branches. With `merged_status: true` each branch also reports whether it is
|
|
28
|
+
# merged into the default branch (one extra git call).
|
|
29
|
+
def branches(merged_status: false)
|
|
30
|
+
merged_names = merged_status ? merged_branch_names : nil
|
|
31
|
+
branch_lines.map do |line|
|
|
32
|
+
Branch.parse(line, default_branch: default_branch, merged_names: merged_names)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def current_branch
|
|
37
|
+
branches.find(&:current?)&.name
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The branch that origin/HEAD points to, else main or master when present locally
|
|
41
|
+
def default_branch
|
|
42
|
+
return @default_branch if defined?(@default_branch)
|
|
43
|
+
|
|
44
|
+
@default_branch = default_ref&.delete_prefix('origin/')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Commits of the current branch, newest first. Empty for a branch without commits.
|
|
48
|
+
def commits(limit: COMMITS_LIMIT)
|
|
49
|
+
@bash.exec('git', 'log', "--max-count=#{limit}", "--format=#{COMMIT_FORMAT}")
|
|
50
|
+
.each_line(chomp: true).reject(&:empty?).map { |line| Commit.parse(line) }
|
|
51
|
+
rescue Git::CommandError
|
|
52
|
+
[]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Full sha of a commit reference, or nil when it does not name a commit
|
|
56
|
+
def resolve_commit(ref)
|
|
57
|
+
stdout, _stderr, success = @bash.capture('git', 'rev-parse', '--verify', '--quiet',
|
|
58
|
+
"#{ref}^{commit}")
|
|
59
|
+
success ? stdout.strip : nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def checkout(branch)
|
|
63
|
+
run('git', 'checkout', branch, '--')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def delete_branch(branch, force: false)
|
|
67
|
+
run('git', 'branch', force ? '-D' : '-d', branch)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def reset(commit, mode:)
|
|
71
|
+
run('git', 'reset', "--#{mode}", commit, '--')
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def branch_lines
|
|
77
|
+
@branch_lines ||= @bash.exec('git', 'for-each-ref', "--format=#{BRANCH_FORMAT}",
|
|
78
|
+
'refs/heads/')
|
|
79
|
+
.each_line(chomp: true).reject(&:empty?)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Ref used to check merge status: origin/HEAD's target, else a local main/master
|
|
83
|
+
def default_ref
|
|
84
|
+
return @default_ref if defined?(@default_ref)
|
|
85
|
+
|
|
86
|
+
@default_ref = remote_default_ref || local_default_branch
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def remote_default_ref
|
|
90
|
+
ref = @bash.exec('git', 'symbolic-ref', '--quiet', '--short',
|
|
91
|
+
'refs/remotes/origin/HEAD').strip
|
|
92
|
+
ref.empty? ? nil : ref
|
|
93
|
+
rescue Git::CommandError
|
|
94
|
+
nil
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def local_default_branch
|
|
98
|
+
names = branch_lines.map { |line| line.split(SEPARATOR, 2).first }
|
|
99
|
+
FALLBACK_DEFAULT_BRANCHES.find { |name| names.include?(name) }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def merged_branch_names
|
|
103
|
+
return nil unless default_ref
|
|
104
|
+
|
|
105
|
+
@bash.exec('git', 'for-each-ref', "--merged=#{default_ref}", '--format=%(refname:short)',
|
|
106
|
+
'refs/heads/')
|
|
107
|
+
.each_line(chomp: true).to_set
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def run(*)
|
|
111
|
+
stdout, stderr, success = @bash.capture(*)
|
|
112
|
+
Result.new(success: success,
|
|
113
|
+
output: [stdout, stderr].map(&:strip).reject(&:empty?).join("\n"))
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/gitflash/ui.rb
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Gitflash
|
|
6
|
+
# Everything the user sees: human text or JSON, menus and confirmations.
|
|
7
|
+
# Menus and confirmations are only shown in a terminal and never in JSON mode.
|
|
8
|
+
#
|
|
9
|
+
# JSON output always uses the same envelope (see schema/v1.json):
|
|
10
|
+
# { schema, command, ok, status, dry_run, plan?, result?, error? }
|
|
11
|
+
class Ui
|
|
12
|
+
JSON_SCHEMA_VERSION = 1
|
|
13
|
+
OK_STATUSES = %w[done planned noop cancelled].freeze
|
|
14
|
+
|
|
15
|
+
def initialize(command:, json: false, yes: false, dry_run: false)
|
|
16
|
+
@command = command
|
|
17
|
+
@json = json
|
|
18
|
+
@yes = yes
|
|
19
|
+
@dry_run = dry_run
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def json?
|
|
23
|
+
@json
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def dry_run?
|
|
27
|
+
@dry_run
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def interactive?
|
|
31
|
+
!json? && $stdin.tty?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Reports the outcome of a command and returns its exit status.
|
|
35
|
+
# `status` is one of done, planned, noop, cancelled or failed.
|
|
36
|
+
def report(status:, text:, plan: nil, result: nil, error: nil)
|
|
37
|
+
if json?
|
|
38
|
+
document = envelope(status: status, plan: plan, result: result, error: error)
|
|
39
|
+
$stdout.puts JSON.generate(document)
|
|
40
|
+
else
|
|
41
|
+
$stdout.puts text
|
|
42
|
+
end
|
|
43
|
+
OK_STATUSES.include?(status) ? 0 : 1
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def error(error)
|
|
47
|
+
if json?
|
|
48
|
+
details = { code: error.code, message: error.message, exit_code: error.exit_code }
|
|
49
|
+
$stdout.puts JSON.generate(envelope(status: error.status, plan: error.plan, error: details))
|
|
50
|
+
else
|
|
51
|
+
warn error.message
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def select(message, choices, **)
|
|
56
|
+
prompt.select(message, choices, **)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def multi_select(message, choices, **)
|
|
60
|
+
prompt.multi_select(message, choices, **)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# True when the action may proceed: --yes was given or the user confirmed in a terminal.
|
|
64
|
+
# Raises ConfirmationRequired, carrying the plan, when nobody can confirm.
|
|
65
|
+
def confirm?(summary, plan:)
|
|
66
|
+
return true if @yes
|
|
67
|
+
|
|
68
|
+
message = "#{summary}\nRe-run with --yes to confirm."
|
|
69
|
+
raise ConfirmationRequired.new(message, plan: plan) unless interactive?
|
|
70
|
+
|
|
71
|
+
prompt.proceed_with_warning(summary) { true } == true
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def envelope(status:, plan: nil, result: nil, error: nil)
|
|
77
|
+
{
|
|
78
|
+
schema: JSON_SCHEMA_VERSION,
|
|
79
|
+
command: @command,
|
|
80
|
+
ok: OK_STATUSES.include?(status),
|
|
81
|
+
status: status,
|
|
82
|
+
dry_run: dry_run?,
|
|
83
|
+
plan: plan,
|
|
84
|
+
result: result,
|
|
85
|
+
error: error
|
|
86
|
+
}.compact
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def prompt
|
|
90
|
+
@prompt ||= Prompt.create
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Raised for invalid arguments or when input is needed but no terminal is available
|
|
5
|
+
class UsageError < Error
|
|
6
|
+
def initialize(message = nil, code: 'invalid_usage', plan: nil)
|
|
7
|
+
super
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def exit_code
|
|
11
|
+
2
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/gitflash/version.rb
CHANGED