class GitProc::ChangeFileHelper

Provides support for prompting the user when the dir/index is dirty.

noinspection RubyControlFlowConversionInspection,RubyClassMethodNamingConvention,RubyInstanceMethodNamingConvention

Attributes

gitlib[R]

Public Class Methods

ask_how_to_handle_changed_files(stat) click to toggle source
# 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) click to toggle source
# 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
new(gitlib) click to toggle source

@param [GitLib] gitlib

# File lib/git-process/changed_file_helper.rb, line 28
def initialize(gitlib)
  @gitlib = gitlib
end
show_changes(type, stat) click to toggle source
# 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

Public Instance Methods

handle_changed_files(stat) click to toggle source
# 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) click to toggle source

noinspection RubyControlFlowConversionInspection

# 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_changes() click to toggle source
# 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