Class: GitProc::ChangeFileHelper
- Inherits:
-
Object
- Object
- GitProc::ChangeFileHelper
- Defined in:
- lib/git-process/changed_file_helper.rb
Overview
Provides support for prompting the user when the dir/index is dirty.
noinspection RubyControlFlowConversionInspection,RubyClassMethodNamingConvention,RubyInstanceMethodNamingConvention
Class Method Summary collapse
- .ask_how_to_handle_changed_files(stat) ⇒ Object
- .ask_how_to_handle_unknown_files(stat) ⇒ Object
- .show_changes(type, stat) ⇒ Object
Instance Method Summary collapse
- #gitlib ⇒ Object
- #handle_changed_files(stat) ⇒ Object
-
#handle_unknown_files(stat) ⇒ Object
noinspection RubyControlFlowConversionInspection.
-
#initialize(gitlib) ⇒ ChangeFileHelper
constructor
A new instance of ChangeFileHelper.
- #offer_to_help_uncommitted_changes ⇒ Object
Constructor Details
#initialize(gitlib) ⇒ ChangeFileHelper
Returns a new instance of ChangeFileHelper.
26 27 28 |
# File 'lib/git-process/changed_file_helper.rb', line 26 def initialize(gitlib) @gitlib = gitlib end |
Class Method Details
.ask_how_to_handle_changed_files(stat) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/git-process/changed_file_helper.rb', line 97 def self.ask_how_to_handle_changed_files(stat) [:added, :modified, :deleted].each { |t| show_changes(t, stat) } resp = ask('Would you like to (c)ommit them or (s)tash them? ') do |q| q.responses[:not_valid] = 'Please respond with either (c)ommit or (s)tash. (Ctl-C to abort.) ' q.case = :down q.validate = /c|s/i end resp == 'c' ? :commit : :stash end |
.ask_how_to_handle_unknown_files(stat) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/git-process/changed_file_helper.rb', line 72 def self.ask_how_to_handle_unknown_files(stat) show_changes(:unknown, stat) resp = ask('Would you like to (a)dd them or (i)gnore them? ') do |q| q.responses[:not_valid] = 'Please respond with either (a)dd or (i)gnore. (Ctl-C to abort.) ' q.case = :down q.validate = /a|i/i end resp == 'a' ? :add : :ignore end |
.show_changes(type, stat) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/git-process/changed_file_helper.rb', line 84 def self.show_changes(type, stat) files = stat.send(type) if type != :deleted files -= stat.deleted end if not files.empty? say("You have <%= color('#{type}', [:underline]) %> files:\n <%= color('#{files.join("\n ")}', [:bold]) %>") end end |
Instance Method Details
#gitlib ⇒ Object
109 110 111 |
# File 'lib/git-process/changed_file_helper.rb', line 109 def gitlib @gitlib end |
#handle_changed_files(stat) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/git-process/changed_file_helper.rb', line 55 def handle_changed_files(stat) if not stat.modified.empty? or not stat.added.empty? or not stat.deleted.empty? resp = ChangeFileHelper.ask_how_to_handle_changed_files(stat) if resp == :commit changed_files = (stat.added + stat.modified - stat.deleted).sort.uniq gitlib.add(changed_files) unless changed_files.empty? gitlib.remove(stat.deleted) unless stat.deleted.empty? gitlib.commit(nil) else gitlib.stash_save end end end |
#handle_unknown_files(stat) ⇒ Object
noinspection RubyControlFlowConversionInspection
45 46 47 48 49 50 51 52 |
# File 'lib/git-process/changed_file_helper.rb', line 45 def handle_unknown_files(stat) if not stat.unknown.empty? resp = ChangeFileHelper.ask_how_to_handle_unknown_files(stat) if resp == :add gitlib.add(stat.unknown) end end end |
#offer_to_help_uncommitted_changes ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/git-process/changed_file_helper.rb', line 31 def offer_to_help_uncommitted_changes stat = gitlib.status if stat.unmerged.empty? handle_unknown_files(stat) handle_changed_files(gitlib.status) # refresh status in case it changed earlier else gitlib.logger.info { "Can not offer to auto-add unmerged files: #{stat.unmerged.inspect}" } raise UncommittedChangesError.new end end |