Class: GitManager

Inherits:
Object
  • Object
show all
Defined in:
lib/git_undo/git_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(history_file) ⇒ GitManager

Returns a new instance of GitManager.



2
3
4
5
# File 'lib/git_undo/git_manager.rb', line 2

def initialize(history_file)
  @history_file = history_file
  @command_list = []
end

Class Method Details

.setupObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/git_undo/git_manager.rb', line 27

def self.setup
  puts "It looks like you haven't run the initial setup. Would you like to do so now (y/n)?"
  if gets.chomp.downcase == 'y'
    puts "This will involve appending an alias to your .bash_profile. Okay to proceed?"
    if gets.chomp.downcase == 'y'
      puts "Thanks!"
      update_bash_profile
    else
      puts "Goodbye!"
    end
  else
    puts "Goodbye!"
  end
end

.update_bash_profileObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/git_undo/git_manager.rb', line 42

def self.update_bash_profile
  alias_exists = false
  # last_line_alias = false
  # position = 0
  # line_count = 0
  # index = 0
  # max_line_count = 0
  file = File.open(File.expand_path('~' + '/.bash_profile'),'r+')

  file.readlines.each do |line|
    # line_count += 1
    if /\A[\s]*alias/.match line
      if /\A[\s]*alias gitundo="HISTFILE=\$HISTFILE gitundo"\n\z/.match line
        alias_exists = true
      end
      # max_line_count = line_count
    # elsif last_line_alias
    #   position = file.pos
    #   last_line_alias = false
    end
  end
  unless alias_exists
    file.write("# Git Undo\n")
    file.write("alias gitundo=\"HISTFILE=$HISTFILE gitundo\"\n")
    puts "Please run `source ~/.bash_profile && cd .` to reload configuration"
  end
  file.close
  # puts max_line_count + 1
  # puts position
end

Instance Method Details

#get_commandsObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/git_undo/git_manager.rb', line 7

def get_commands
  File.open(@history_file) do |file|
    file.each do |line|
      # find last git command
      if /\Agit / =~ line
        @command_list << line.chomp
      end
    end
  end
end

#last_commandObject



18
19
20
# File 'lib/git_undo/git_manager.rb', line 18

def last_command
  @command_list.last
end

#runObject



22
23
24
25
# File 'lib/git_undo/git_manager.rb', line 22

def run
  get_commands
  puts "Last git command was: `#{last_command}`"
end