chbr 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/chbr +6 -0
  3. data/lib/chbr.rb +133 -0
  4. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0753c61757a5afd8f315975f16940989a83dacd9f39dda1693bf89a6e235cbbc
4
+ data.tar.gz: a9e49efd69b2a261787500c6ce2f3cd6df86e743d67cbd3313c9ea2022eb6efa
5
+ SHA512:
6
+ metadata.gz: 7b7698cf9efc8e8353e3a6583e34a630c56e792362b88364871387dc88a57f1d7f84e4f19cdb04eae73145a374fd527c0cf563c813da1ebe7977f10b27068d25
7
+ data.tar.gz: ad249f00f6b185b819c9c046c372b667830de4f46e6ae427c96b15ac1b9615b95901b2162267cf8c5f5b47f67b2a4293e292a9bd21316f52f4d330e7dc2c431a
data/bin/chbr ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'chbr'
5
+
6
+ Chbr.run
data/lib/chbr.rb ADDED
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'git'
4
+ require 'optparse'
5
+
6
+ # Class that handles the main functionality of the tool. It allows the user to
7
+ # checkout branches, delete branches and purge branches. It also allows the
8
+ # user to set the path to the repository, the timeout for git commands and
9
+ # whether the panel should reopen after an action is performed.
10
+ class Chbr
11
+ DEFAULT_TIMEOUT = 5
12
+
13
+ attr_accessor :repo_path, :reopen_after_action, :repo
14
+
15
+ def self.run
16
+ new.run
17
+ end
18
+
19
+ def run
20
+ Git.config.timeout = DEFAULT_TIMEOUT
21
+
22
+ @repo_path = Dir.pwd
23
+
24
+ @reopen_after_action = true
25
+
26
+ parse_options
27
+
28
+ another_repo = @repo_path != Dir.pwd
29
+ current_path = Dir.pwd
30
+
31
+ go_to_repo(@repo_path) if another_repo
32
+
33
+ begin
34
+ @repo = Git.open(@repo_path)
35
+ rescue ArgumentError => e
36
+ puts "Error: #{e.message}"
37
+
38
+ return
39
+ end
40
+
41
+ open_panel
42
+
43
+ go_to_repo(current_path) if another_repo
44
+ end
45
+
46
+ def go_to_repo(path)
47
+ Dir.chdir(path)
48
+ end
49
+
50
+ def parse_options
51
+ OptionParser.new do |opts|
52
+ opts.banner = 'Usage: chbr [path_to_repository]'
53
+
54
+ opts.on('-p PATH', '--path PATH', 'Path to the git repository') do |path|
55
+ @repo_path = path
56
+ end
57
+
58
+ opts.on(
59
+ '--disable-reopen',
60
+ 'Disable reopening the panel after an action is performed (Defaults to true)'
61
+ ) do
62
+ @reopen_after_action = false
63
+ end
64
+
65
+ opts.on(
66
+ 't', '--timeout TIMEOUT',
67
+ 'Set the timeout for git commands in seconds (Defaults to 5)'
68
+ ) do |timeout|
69
+ Git.config.timeout = timeout.to_i
70
+ end
71
+ end.parse!
72
+ end
73
+
74
+ def open_panel
75
+ puts '[ENTER] to checkout branch, [X] to delete branch, [P] to purge branches'
76
+
77
+ result = `
78
+ git branch | fzf --bind "enter:accept,X:become(echo {}'%%%delete')+abort,P:accept+become(echo {}'%%%purge')+abort" --height 40% --layout reverse|
79
+ tr -d "*[:space:]|+[:space:]"
80
+ `
81
+
82
+ return puts 'No branch selected' if result.empty?
83
+
84
+ branch, action = result.split('%%%')
85
+
86
+ begin
87
+ case action
88
+ when 'delete'
89
+ puts "Are you sure you want to delete branch \"#{branch}\"? (y/n)"
90
+
91
+ deleted = false
92
+
93
+ deleted = delete_branch(branch) if gets.chomp == 'y'
94
+
95
+ open_panel if @reopen_after_action && deleted
96
+ when 'purge'
97
+ puts "This action will delete all branches on this repository except for \"#{branch}\". Are you sure? (y/n)"
98
+
99
+ purge_branches(branch) if gets.chomp == 'y'
100
+ else
101
+ checkout_branch(result)
102
+ end
103
+ rescue StandardError => e
104
+ puts "Error: #{e.message}"
105
+ end
106
+ end
107
+
108
+ def delete_branch(branch)
109
+ if @repo.current_branch == branch
110
+ puts 'Cannot delete checked out branch'
111
+
112
+ return false
113
+ end
114
+
115
+ repo.branch(branch).delete
116
+
117
+ true
118
+ end
119
+
120
+ def purge_branches(except)
121
+ checkout_branch(except) if @repo.current_branch != except
122
+
123
+ @repo.branches.each do |branch|
124
+ next if branch.name == except
125
+
126
+ delete_branch(branch.name)
127
+ end
128
+ end
129
+
130
+ def checkout_branch(branch)
131
+ @repo.branch(branch).checkout
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chbr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - thestbar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: optparse
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
41
+ description: A simple CLI tool to change branches in a git repositoy. You can also
42
+ delete local branches.
43
+ email: stavrosbarousis@gmail.com
44
+ executables:
45
+ - chbr
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - bin/chbr
50
+ - lib/chbr.rb
51
+ homepage: https://rubygems.org/gems/chbr
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.5.9
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Change branch tool
74
+ test_files: []