Class: GitReverser

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

Constant Summary collapse

VALID_COMMANDS =
['add','commit','merge','checkout','co', 'rebase']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ GitReverser

Returns a new instance of GitReverser.



6
7
8
# File 'lib/git_undo/git_reverser.rb', line 6

def initialize(arguments)
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

Instance Method Details

#reverse_addObject



10
11
12
# File 'lib/git_undo/git_reverser.rb', line 10

def reverse_add
  "git reset #{arguments}"
end

#reverse_checkoutObject



22
23
24
25
26
27
28
29
30
# File 'lib/git_undo/git_reverser.rb', line 22

def reverse_checkout
  undo_command = "git checkout -"
  if arguments.start_with?('-b')
    #also delete branch
    branch_name = arguments.split.last
    undo_command += " && git branch -D #{branch_name}"
  end
  undo_command
end

#reverse_commitObject



14
15
16
# File 'lib/git_undo/git_reverser.rb', line 14

def reverse_commit
  'git reset --soft HEAD~'
end

#reverse_mergeObject



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

def reverse_merge
  'git reset --merge ORIG_HEAD'
end

#reverse_rebaseObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git_undo/git_reverser.rb', line 32

def reverse_rebase
  if !arguments.include?('-i')
    current_branch_command = 'git rev-parse --abbrev-ref HEAD'
    branch_name = %x[ #{current_branch_command} ].chomp

    fetch_old_state = "git checkout #{branch_name}@{1}"
    delete_branch = "git branch -D #{branch_name}"
    recreate_branch = "git checkout -b #{branch_name}"

    "#{fetch_old_state} && #{delete_branch} && #{recreate_branch}"
  end
end