git-branch-selector 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dd8312a533e0e4197b38cc768fc1afe2fd53ec325121d75260051cd15c5cf37e
4
+ data.tar.gz: a78388d8bb9f5396fedc38a7c677849cf6000db78dcdf01d4f9244a22d5972ea
5
+ SHA512:
6
+ metadata.gz: 4b8032178c720e5cf1ea0516408a49d76b1d688188e72a4346a743ebe47508bd54dbb08bd5ffac4bb81d17ecad2a8ad9f05f6ea35890b14e0e7258e3a244b436
7
+ data.tar.gz: 9783d3ec925e46202c7e3422ad4ff7842235cc2f90d4c7d75bb591d6e2890ba017edfa5ab0c0b4dd2bdbf85fd6fa8201fae5f605614403477530578ff0f9ce71
@@ -0,0 +1,54 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ # require 'pry'
5
+ require 'git-branch-selector'
6
+
7
+ options = {
8
+ count: 15,
9
+ list: false,
10
+ copy: false
11
+ }
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = 'Usage: gbs.rb [options].'
15
+ opts.banner << ' Passing no options will change branches on select.'
16
+ opts.banner << ' Default count is 15.'
17
+ opts.separator ''
18
+ opts.separator 'Specific options:'
19
+
20
+ opts.on('-n COUNT', '--num=COUNT', Integer, 'Number of branches to display') do |count|
21
+ options[:count] = count
22
+ end
23
+
24
+ opts.on('-l', '--list', 'List branches') do |bool|
25
+ options[:list] = !!bool
26
+ end
27
+
28
+ opts.on('-c', '--copy', 'Copy selected branch name') do |bool|
29
+ options[:copy] = !!bool
30
+ end
31
+ end.parse!
32
+
33
+ begin
34
+ if options[:list] && options[:copy]
35
+ p 'List (-l) and copy (-c) cannot be used together. Please choose only one option'
36
+ return
37
+ end
38
+
39
+ flag =
40
+ if options[:list]
41
+ 'list'
42
+ elsif options[:copy]
43
+ 'copy'
44
+ else
45
+ nil
46
+ end
47
+
48
+ GitBranchSelector.new(
49
+ flag: flag,
50
+ branch_lookback_count: options[:count]
51
+ ).build_menu
52
+ rescue TTY::Reader::InputInterrupt
53
+ exit 1
54
+ end
@@ -0,0 +1,66 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'tty-prompt'
4
+ require 'open3'
5
+ require 'pry'
6
+
7
+ class GitBranchSelector
8
+ attr_reader :flag, :branch_lookback_count
9
+
10
+ def initialize(flag:, branch_lookback_count:)
11
+ @flag = flag
12
+ @branch_lookback_count = branch_lookback_count
13
+ end
14
+
15
+ def build_menu
16
+ return show_list if flag == 'list'
17
+
18
+ if cleaned_branches.empty?
19
+ puts %q(
20
+ No branches to list. Ensure you're using this within a Git repository.
21
+ )
22
+ return
23
+ end
24
+
25
+ selected_option = TTY::Prompt.new.select(
26
+ "Pick a branch",
27
+ cycle: true,
28
+ symbols: { marker: '☾' },
29
+ per_page: cleaned_branches.length,
30
+ filter: true,
31
+ help: '(Arrows, numbers, type to filter, enter to select)',
32
+ show_help: :always
33
+ ) do |menu|
34
+ menu.enum ':'
35
+
36
+ cleaned_branches.each_with_index do |b, index|
37
+ menu.choice b, index + 1
38
+ end
39
+ end
40
+
41
+ branch_name = cleaned_branches[selected_option - 1]
42
+
43
+ cmd = flag == 'copy' ? "echo #{branch_name} | pbcopy" : "git checkout #{branch_name}"
44
+
45
+ Open3.capture3(cmd)
46
+ end
47
+
48
+ def show_list
49
+ cleaned_branches.each_with_index do |b, i|
50
+ puts "#{i + 1}: #{b}"
51
+ end
52
+ end
53
+
54
+ def cleaned_branches
55
+ @cleaned_branches ||= get_branches.split("\n").map do |branch|
56
+ branch.split(/\s+\d+\t\s+/).last
57
+ end
58
+ end
59
+
60
+ def get_branches
61
+ get_branches_cmd = "git log -g --grep-reflog 'checkout:' --format='%gs' | sed -E 's/checkout: moving from .* to//g' | awk '!seen[$0]++' | head -n #{branch_lookback_count} | cat -n"
62
+
63
+ recent_branches, = Open3.capture3(get_branches_cmd)
64
+ recent_branches
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-branch-selector
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Back
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optparse
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: open3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-prompt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.23'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.23'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.14'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ description: This is a Git branch selection tool. It provides a way to quickly look
70
+ back at your most recently checked-out branches. You can list the branches, use
71
+ the selector tool to add the branch name to your clipboard, or checkout to the selected
72
+ branch.
73
+ email:
74
+ - ryanpback@gmail.com
75
+ executables:
76
+ - git-branch-selector
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - bin/git-branch-selector
81
+ - lib/git-branch-selector.rb
82
+ homepage: https://github.com/ryanpback/git-branch-selector
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.7.6
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.1.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A Git Branch Selector for your most recently used branches.
105
+ test_files: []