Class: Chbr
- Inherits:
-
Object
- Object
- Chbr
- 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
-
#reopen_after_action ⇒ Object
Returns the value of attribute reopen_after_action.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#repo_path ⇒ Object
Returns the value of attribute repo_path.
Class Method Summary collapse
Instance Method Summary collapse
- #checkout_branch(branch) ⇒ Object
- #delete_branch(branch) ⇒ Object
- #go_to_repo(path) ⇒ Object
- #open_panel ⇒ Object
- #parse_options ⇒ Object
- #purge_branches(except) ⇒ Object
- #run ⇒ Object
Instance Attribute Details
#reopen_after_action ⇒ Object
Returns the value of attribute reopen_after_action.
13 14 15 |
# File 'lib/chbr.rb', line 13 def reopen_after_action @reopen_after_action end |
#repo ⇒ Object
Returns the value of attribute repo.
13 14 15 |
# File 'lib/chbr.rb', line 13 def repo @repo end |
#repo_path ⇒ Object
Returns the value of attribute repo_path.
13 14 15 |
# File 'lib/chbr.rb', line 13 def repo_path @repo_path end |
Class Method Details
.run ⇒ Object
15 16 17 |
# File 'lib/chbr.rb', line 15 def self.run new.run end |
Instance Method Details
#checkout_branch(branch) ⇒ Object
130 131 132 |
# File 'lib/chbr.rb', line 130 def checkout_branch(branch) @repo.branch(branch).checkout end |
#delete_branch(branch) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chbr.rb', line 108 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
46 47 48 |
# File 'lib/chbr.rb', line 46 def go_to_repo(path) Dir.chdir(path) end |
#open_panel ⇒ Object
74 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 |
# File 'lib/chbr.rb', line 74 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% --layout reverse| 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.}" end end |
#parse_options ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/chbr.rb', line 50 def OptionParser.new do |opts| opts. = '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
120 121 122 123 124 125 126 127 128 |
# File 'lib/chbr.rb', line 120 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 |
#run ⇒ Object
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 |
# File 'lib/chbr.rb', line 19 def run Git.config.timeout = DEFAULT_TIMEOUT @repo_path = Dir.pwd @reopen_after_action = true 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.}" return end open_panel go_to_repo(current_path) if another_repo end |