git_selector 0.1.5 → 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 +1 -1
 - data/lib/git_prompt.rb +46 -19
 - data/lib/git_selector/version.rb +1 -1
 - data/lib/git_selector.rb +1 -1
 - data/lib/treeish_extractor.rb +37 -0
 - metadata +3 -3
 - 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
    
    
    
        data/lib/git_prompt.rb
    CHANGED
    
    | 
         @@ -3,48 +3,75 @@ 
     | 
|
| 
       3 
3 
     | 
    
         
             
            require 'git'
         
     | 
| 
       4 
4 
     | 
    
         
             
            require 'tty-prompt'
         
     | 
| 
       5 
5 
     | 
    
         
             
            require 'logger'
         
     | 
| 
       6 
     | 
    
         
            -
            require_relative './ 
     | 
| 
      
 6 
     | 
    
         
            +
            require_relative './treeish_extractor'
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
            class GitPrompt
         
     | 
| 
      
 9 
     | 
    
         
            +
              SELECT_OPTIONS_PER_PAGE = 10
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
       9 
11 
     | 
    
         
             
              attr_reader :git, :extractor, :prompt, :logger
         
     | 
| 
       10 
12 
     | 
    
         | 
| 
       11 
13 
     | 
    
         
             
              def initialize(git: nil)
         
     | 
| 
       12 
14 
     | 
    
         
             
                @git = git || Git.open(Dir.pwd)
         
     | 
| 
       13 
     | 
    
         
            -
                @extractor =  
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
                define_key_events
         
     | 
| 
       17 
     | 
    
         
            -
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
                @extractor = TreeishExtractor.new(git: git)
         
     | 
| 
      
 16 
     | 
    
         
            +
            		@prompt = TTY::Prompt.new(quiet: true)
         
     | 
| 
       18 
17 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                 
     | 
| 
       21 
     | 
    
         
            -
                prompt.select(message, filter: false, 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
         
     | 
| 
      
 18 
     | 
    
         
            +
            		define_key_events
         
     | 
| 
      
 19 
     | 
    
         
            +
                select_tag(extractor.recent_branch_names)
         
     | 
| 
       28 
20 
     | 
    
         
             
              end
         
     | 
| 
       29 
21 
     | 
    
         | 
| 
       30 
22 
     | 
    
         
             
              def define_key_events
         
     | 
| 
       31 
23 
     | 
    
         
             
                prompt.on(:keypress) do |event|
         
     | 
| 
       32 
     | 
    
         
            -
                  if event.value ==  
     | 
| 
      
 24 
     | 
    
         
            +
                  if event.value == 'q'
         
     | 
| 
      
 25 
     | 
    
         
            +
                    exit
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  if event.value == 'j'
         
     | 
| 
       33 
29 
     | 
    
         
             
                    prompt.trigger(:keydown)
         
     | 
| 
       34 
30 
     | 
    
         
             
                  end
         
     | 
| 
       35 
31 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
                  if event.value ==  
     | 
| 
      
 32 
     | 
    
         
            +
                  if event.value == 'k'
         
     | 
| 
       37 
33 
     | 
    
         
             
                    prompt.trigger(:keyup)
         
     | 
| 
       38 
34 
     | 
    
         
             
                  end
         
     | 
| 
       39 
35 
     | 
    
         | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
      
 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)
         
     | 
| 
       42 
45 
     | 
    
         
             
                  end
         
     | 
| 
       43 
46 
     | 
    
         
             
                end
         
     | 
| 
       44 
47 
     | 
    
         
             
              end
         
     | 
| 
       45 
48 
     | 
    
         | 
| 
       46 
49 
     | 
    
         
             
              private
         
     | 
| 
       47 
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 
     | 
    
         
            +
             
     | 
| 
       48 
75 
     | 
    
         
             
              def checkout(tag_name)
         
     | 
| 
       49 
76 
     | 
    
         
             
                git.checkout(tag_name)
         
     | 
| 
       50 
77 
     | 
    
         
             
              rescue Git::GitExecuteError => e
         
     | 
    
        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
         
     | 
| 
         @@ -76,7 +76,7 @@ files: 
     | 
|
| 
       76 
76 
     | 
    
         
             
            - lib/git_prompt.rb
         
     | 
| 
       77 
77 
     | 
    
         
             
            - lib/git_selector.rb
         
     | 
| 
       78 
78 
     | 
    
         
             
            - lib/git_selector/version.rb
         
     | 
| 
       79 
     | 
    
         
            -
            - lib/ 
     | 
| 
      
 79 
     | 
    
         
            +
            - lib/treeish_extractor.rb
         
     | 
| 
       80 
80 
     | 
    
         
             
            homepage: https://github.com/suzukimilanpaak/git_selector
         
     | 
| 
       81 
81 
     | 
    
         
             
            licenses:
         
     | 
| 
       82 
82 
     | 
    
         
             
            - MIT
         
     | 
    
        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 
     | 
    
         
            -
             
     |