Class: GitDD

Inherits:
Object
  • Object
show all
Defined in:
lib/git-dd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#promptObject

Returns the value of attribute prompt.



8
9
10
# File 'lib/git-dd.rb', line 8

def prompt
  @prompt
end

Instance Method Details

#run(test_prompt = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# File 'lib/git-dd.rb', line 10

def run(test_prompt = nil)
  branch_names = `git branch`
  return if $?.exitstatus != 0

  branch_names = branch_names.split("\n")

  branches_with_more_info = `git branch -vv`
  begin
    branches_with_more_info = branches_with_more_info.split("\n")
  rescue
    branches_with_more_info = `git branch`
    branches_with_more_info = branches_with_more_info.split("\n")
  end

  return if branch_names.size != branches_with_more_info.size

  branches = {}
  branch_names.each_with_index { |b, i| branches[b] = branches_with_more_info[i] }

  if branches.size == 1
    return print(ONLY_ONE_BRANCH)
  end

  branches = branches.select { |k, v| k != current_branch_with_mark && !k.include?("* (HEAD detached at") }

  puts "Current branch is: #{current_branch.color(:green)}"

  if test_prompt
    prompt = test_prompt
  else
    prompt = TTY::Prompt.new(interrupt: :exit)
  end

  prompt.on(:keypress) do |event|
    if event.value == 'q'
      return
    end
  end

  branches_to_delete = prompt.multi_select("Choose branches to delete:", per_page: 20, help: '',echo: false) do |menu|
    branches.each { |k, v| menu.choice v, k}
  end

  if branches_to_delete.size == 0
    return print(NO_BRANCH_SELECTED)
  end

  branches_to_delete.each { |b| puts b.color(:red) }

  ensure_delete = !prompt.no?('Are you sure?')

  if ensure_delete
    branches_to_delete.each do |b|
      puts `git branch -D #{b}`.chomp.color(:yellow)
    end
  end

  return branches_to_delete
end