Class: GitProc::ChangeFileHelper

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gitlib) ⇒ ChangeFileHelper

Returns a new instance of ChangeFileHelper.

Parameters:



28
29
30
# File 'lib/git-process/changed_file_helper.rb', line 28

def initialize(gitlib)
  @gitlib = gitlib
end

Instance Attribute Details

#gitlibObject (readonly)

Returns the value of attribute gitlib.



25
26
27
# File 'lib/git-process/changed_file_helper.rb', line 25

def gitlib
  @gitlib
end

Class Method Details

.ask_how_to_handle_changed_files(stat) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/git-process/changed_file_helper.rb', line 99

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/git-process/changed_file_helper.rb', line 74

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



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git-process/changed_file_helper.rb', line 86

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

#handle_changed_files(stat) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/git-process/changed_file_helper.rb', line 57

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



47
48
49
50
51
52
53
54
# File 'lib/git-process/changed_file_helper.rb', line 47

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_changesObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git-process/changed_file_helper.rb', line 33

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