Class: Chbr

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

Overview

Class that handles the main functionality of the tool. It allows the user to checkout branches, delete branches and purge branches. It also allows the user to set the path to the repository, the timeout for git commands and whether the panel should reopen after an action is performed.

Constant Summary collapse

DEFAULT_TIMEOUT =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reopen_after_actionObject

Returns the value of attribute reopen_after_action.



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

def reopen_after_action
  @reopen_after_action
end

#repoObject

Returns the value of attribute repo.



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

def repo
  @repo
end

#repo_pathObject

Returns the value of attribute repo_path.



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

def repo_path
  @repo_path
end

Class Method Details

.runObject



16
17
18
# File 'lib/chbr.rb', line 16

def self.run
  new.run
end

Instance Method Details

#checkout_branch(branch) ⇒ Object



131
132
133
# File 'lib/chbr.rb', line 131

def checkout_branch(branch)
  @repo.branch(branch).checkout
end

#delete_branch(branch) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chbr.rb', line 109

def delete_branch(branch)
  if @repo.current_branch == branch
    puts 'Cannot delete checked out branch'

    return false
  end

  repo.branch(branch).delete

  true
end

#go_to_repo(path) ⇒ Object



47
48
49
# File 'lib/chbr.rb', line 47

def go_to_repo(path)
  Dir.chdir(path)
end

#open_panelObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/chbr.rb', line 75

def open_panel
  puts '[ENTER] to checkout branch, [X] to delete branch, [P] to purge branches'

  result = `
    git branch | fzf --bind "enter:accept,X:become(echo {}'%%%delete')+abort,P:accept+become(echo {}'%%%purge')+abort" --height 40% |
    tr -d "*[:space:]|+[:space:]"
  `

  return puts 'No branch selected' if result.empty?

  branch, action = result.split('%%%')

  begin
    case action
    when 'delete'
      puts "Are you sure you want to delete branch \"#{branch}\"? (y/n)"

      deleted = false

      deleted = delete_branch(branch) if gets.chomp == 'y'

      open_panel if @reopen_after_action && deleted
    when 'purge'
      puts "This action will delete all branches on this repository except for \"#{branch}\". Are you sure? (y/n)"

      purge_branches(branch) if gets.chomp == 'y'
    else
      checkout_branch(result)
    end
  rescue StandardError => e
    puts "Error: #{e.message}"
  end
end

#parse_optionsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chbr.rb', line 51

def parse_options
  OptionParser.new do |opts|
    opts.banner = 'Usage: chbr [path_to_repository]'

    opts.on('-p PATH', '--path PATH', 'Path to the git repository') do |path|
      @repo_path = path
    end

    opts.on(
      '--disable-reopen',
      'Disable reopening the panel after an action is performed (Defaults to true)'
    ) do
      @reopen_after_action = false
    end

    opts.on(
      't', '--timeout TIMEOUT',
      'Set the timeout for git commands in seconds (Defaults to 5)'
    ) do |timeout|
      Git.config.timeout = timeout.to_i
    end
  end.parse!
end

#purge_branches(except) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/chbr.rb', line 121

def purge_branches(except)
  checkout_branch(except) if @repo.current_branch != except

  @repo.branches.each do |branch|
    next if branch.name == except

    delete_branch(branch.name)
  end
end

#runObject



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
# File 'lib/chbr.rb', line 20

def run
  Git.config.timeout = DEFAULT_TIMEOUT

  @repo_path = Dir.pwd

  @reopen_after_action = true

  parse_options

  another_repo = @repo_path != Dir.pwd
  current_path = Dir.pwd

  go_to_repo(@repo_path) if another_repo

  begin
    @repo = Git.open(@repo_path)
  rescue ArgumentError => e
    puts "Error: #{e.message}"

    return
  end

  open_panel

  go_to_repo(current_path) if another_repo
end