git_selector 0.1.4 → 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.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -4
- data/bin/console +14 -0
- data/git_selector.gemspec +1 -1
- data/lib/git_prompt.rb +81 -0
- data/lib/git_selector/version.rb +1 -1
- data/lib/git_selector.rb +2 -2
- data/lib/treeish_extractor.rb +37 -0
- metadata +6 -5
- data/lib/selector.rb +0 -54
- data/lib/tag_extractor.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d212d92587ff373597832eb02bd269c3af4a933e7d849aeadd6f7682b1c8335f
|
4
|
+
data.tar.gz: 6f5294ff9a530f126b9aa380fd6a1ff77a81987a4096a89be6ed4c9822739beb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c3a06122986717b4179a52e7ff2d1eedcc7ee91a617cbe44bcd4c5262f9cc29ae30bc2ddf7ef4d8336a00fec2bf769c1e06fa1640197f8bc63678c1993b8d8d
|
7
|
+
data.tar.gz: c6d13b22ffe66643c48a82fc579bacb2cc27793f2ffec5fc7dcbf12195aec98a206cae40d0eaaa2b12f0540caf94559480f47fdafdb2d91c1c43de006a7c81c9
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
git_selector (0.
|
4
|
+
git_selector (0.2.0)
|
5
5
|
git
|
6
|
-
tty-prompt
|
6
|
+
tty-prompt
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -51,8 +51,6 @@ GEM
|
|
51
51
|
tty-prompt (0.23.1)
|
52
52
|
pastel (~> 0.8)
|
53
53
|
tty-reader (~> 0.8)
|
54
|
-
tty-prompt-vim (0.1.0)
|
55
|
-
tty-prompt (~> 0.16)
|
56
54
|
tty-reader (0.9.0)
|
57
55
|
tty-cursor (~> 0.7)
|
58
56
|
tty-screen (~> 0.8)
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "git_selector"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/git_selector.gemspec
CHANGED
data/lib/git_prompt.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'git'
|
4
|
+
require 'tty-prompt'
|
5
|
+
require 'logger'
|
6
|
+
require_relative './treeish_extractor'
|
7
|
+
|
8
|
+
class GitPrompt
|
9
|
+
SELECT_OPTIONS_PER_PAGE = 10
|
10
|
+
|
11
|
+
attr_reader :git, :extractor, :prompt, :logger
|
12
|
+
|
13
|
+
def initialize(git: nil)
|
14
|
+
@git = git || Git.open(Dir.pwd)
|
15
|
+
@extractor = TreeishExtractor.new(git: git)
|
16
|
+
@prompt = TTY::Prompt.new(quiet: true)
|
17
|
+
|
18
|
+
define_key_events
|
19
|
+
select_tag(extractor.recent_branch_names)
|
20
|
+
end
|
21
|
+
|
22
|
+
def define_key_events
|
23
|
+
prompt.on(:keypress) do |event|
|
24
|
+
if event.value == 'q'
|
25
|
+
exit
|
26
|
+
end
|
27
|
+
|
28
|
+
if event.value == 'j'
|
29
|
+
prompt.trigger(:keydown)
|
30
|
+
end
|
31
|
+
|
32
|
+
if event.value == 'k'
|
33
|
+
prompt.trigger(:keyup)
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
if event.value == 'b'
|
38
|
+
clear_prompt
|
39
|
+
select_tag(extractor.recent_branch_names)
|
40
|
+
end
|
41
|
+
|
42
|
+
if event.value == 't'
|
43
|
+
clear_prompt
|
44
|
+
select_tag(extractor.recent_tag_names)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def clear_prompt
|
52
|
+
# 2 is for the prompt message
|
53
|
+
prompt.print TTY::Cursor.clear_lines(SELECT_OPTIONS_PER_PAGE + 2, :up)
|
54
|
+
end
|
55
|
+
|
56
|
+
def select_tag(treeish_names)
|
57
|
+
message = <<~MSG.chomp
|
58
|
+
j: down, k: up, q: quit, Enter: choose tag
|
59
|
+
[b] branch mode [t] tag mode
|
60
|
+
MSG
|
61
|
+
|
62
|
+
if treeish_names.size > 0
|
63
|
+
prompt.select(message, filter: false, per_page: SELECT_OPTIONS_PER_PAGE) do |menu|
|
64
|
+
treeish_names.each do |tag_name|
|
65
|
+
menu.choice tag_name, -> { checkout(tag_name) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
else
|
69
|
+
prompt.ok 'No tags were found'
|
70
|
+
end
|
71
|
+
rescue TTY::Reader::InputInterrupt => e
|
72
|
+
exit
|
73
|
+
end
|
74
|
+
|
75
|
+
def checkout(tag_name)
|
76
|
+
git.checkout(tag_name)
|
77
|
+
rescue Git::GitExecuteError => e
|
78
|
+
prompt.error(e.message)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
data/lib/git_selector/version.rb
CHANGED
data/lib/git_selector.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'git'
|
4
|
+
require 'open3'
|
5
|
+
|
6
|
+
class TreeishExtractor
|
7
|
+
attr_reader :git
|
8
|
+
|
9
|
+
def initialize(git: nil)
|
10
|
+
@git = git || Git.open(Dir.pwd)
|
11
|
+
end
|
12
|
+
|
13
|
+
def recent_tag_names
|
14
|
+
# TODO unlimit number of tags when arg is given
|
15
|
+
cmd = "git describe --tags $(git rev-list --tags --max-count=1000)"
|
16
|
+
get_treeish_names(cmd)
|
17
|
+
end
|
18
|
+
|
19
|
+
def recent_branch_names
|
20
|
+
# TODO limit number of branches when arg is not given
|
21
|
+
cmd = 'git branch --sort=-committerdate'
|
22
|
+
get_treeish_names(cmd)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def get_treeish_names(cmd)
|
28
|
+
names = []
|
29
|
+
_stdin, stdout, _stderr, _wait_thr = Open3.popen3(cmd)
|
30
|
+
stdout.each(sep="\n") do |line|
|
31
|
+
# reject rev without a tag
|
32
|
+
# current branch has '* ' to indicate it's selected
|
33
|
+
names << line.strip.sub('* ', '') unless line =~ /-[\d]+-[a-z0-9]{8,}$/
|
34
|
+
end
|
35
|
+
names
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: git_selector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuya Suzuki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin/release
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: git
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: tty-prompt
|
28
|
+
name: tty-prompt
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -69,13 +69,14 @@ files:
|
|
69
69
|
- LICENSE.txt
|
70
70
|
- README.md
|
71
71
|
- Rakefile
|
72
|
+
- bin/console
|
72
73
|
- bin/release/gits
|
73
74
|
- bin/setup
|
74
75
|
- git_selector.gemspec
|
76
|
+
- lib/git_prompt.rb
|
75
77
|
- lib/git_selector.rb
|
76
78
|
- lib/git_selector/version.rb
|
77
|
-
- lib/
|
78
|
-
- lib/tag_extractor.rb
|
79
|
+
- lib/treeish_extractor.rb
|
79
80
|
homepage: https://github.com/suzukimilanpaak/git_selector
|
80
81
|
licenses:
|
81
82
|
- MIT
|
data/lib/selector.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'git'
|
4
|
-
require 'tty/prompt/vim'
|
5
|
-
require 'logger'
|
6
|
-
require_relative './tag_extractor'
|
7
|
-
|
8
|
-
class Selector
|
9
|
-
attr_reader :git, :extractor, :prompt, :logger
|
10
|
-
|
11
|
-
def initialize(git: nil)
|
12
|
-
@git = git || Git.open(Dir.pwd)
|
13
|
-
@extractor = TagExtractor.new(git: git)
|
14
|
-
@prompt = TTY::Prompt.new
|
15
|
-
|
16
|
-
define_key_events
|
17
|
-
end
|
18
|
-
|
19
|
-
def select_tag
|
20
|
-
message = 'j/↓: down, k/↑: up, Enter: choose tag, Type to filter tags'
|
21
|
-
prompt.select(message, filter: true, per_page: 10) do |menu|
|
22
|
-
extractor.recent_tag_names.each do |tag_name|
|
23
|
-
menu.choice tag_name, -> { checkout(tag_name) }
|
24
|
-
end
|
25
|
-
end
|
26
|
-
rescue TTY::Reader::InputInterrupt => e
|
27
|
-
exit
|
28
|
-
end
|
29
|
-
|
30
|
-
def define_key_events
|
31
|
-
prompt.on(:keypress) do |event|
|
32
|
-
if event.value == "j"
|
33
|
-
prompt.trigger(:keydown)
|
34
|
-
end
|
35
|
-
|
36
|
-
if event.value == "k"
|
37
|
-
prompt.trigger(:keyup)
|
38
|
-
end
|
39
|
-
|
40
|
-
if event.value == "q"
|
41
|
-
exit
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def checkout(tag_name)
|
49
|
-
git.checkout(tag_name)
|
50
|
-
rescue Git::GitExecuteError => e
|
51
|
-
prompt.error(e.message)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
data/lib/tag_extractor.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'git'
|
4
|
-
require 'open3'
|
5
|
-
|
6
|
-
class TagExtractor
|
7
|
-
attr_reader :git, :shas_names
|
8
|
-
|
9
|
-
def initialize(git: nil)
|
10
|
-
@git = git || Git.open(Dir.pwd)
|
11
|
-
end
|
12
|
-
|
13
|
-
def recent_tag_names
|
14
|
-
if head_changed?
|
15
|
-
tag_names = []
|
16
|
-
# TODO unlimit tags when arg is given
|
17
|
-
cmd = "git describe --tags $(git rev-list --tags --max-count=1000)"
|
18
|
-
_stdin, stdout, _stderr, _wait_thr = Open3.popen3(cmd)
|
19
|
-
stdout.each(sep="\n") do |line|
|
20
|
-
# reject rev without a tag
|
21
|
-
tag_names << line.chomp unless line =~ /-[\d]+-[a-z0-9]{8,}$/
|
22
|
-
end
|
23
|
-
@tag_names = tag_names
|
24
|
-
else
|
25
|
-
@tag_names
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def shas_names
|
32
|
-
if head_changed?
|
33
|
-
@shas_names = git.tags.each_with_object({}) { |t, sum| (sum[t.sha] ||= []) << t.name }
|
34
|
-
else
|
35
|
-
@shas_names
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def head_changed?
|
40
|
-
changed = (@head_sha != git.log(1).first.sha)
|
41
|
-
@head_sha = git.log(1).first.sha
|
42
|
-
changed
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|