git-fancy 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03a33152dbf41fc359d422a9e44cdeae8e2f1e9205bc6515525155a90cb98f19
4
- data.tar.gz: 8b2c242b5cbe8e9cfdebe41a17658d169ed65a609731b865ce9dc12c24597e30
3
+ metadata.gz: 685d5e178fb8233c3c22be3c24618b164c8e11ef96b809dada54ee831ffdd04e
4
+ data.tar.gz: ce0556c30dfb4e1b3ec8ff83b3f1fc19fb10c890696ec609358f2ce7c81bb4c8
5
5
  SHA512:
6
- metadata.gz: e99664ee065b29e79c45ad6eeddca64641a442fbe8dafe75ad414a332bc8f369830e273350fb72735db8cb518bff9b1c0a89bb87fd97a720fae0e1203a581ab4
7
- data.tar.gz: b6c90f97a02d093ba58beda95d95cc42eafc6827cae124dbfb773c18fd0e1f7f30cf2b79aa4150975ef6d05ab462e27383785a5e1bbc199cf8e2603303701acb
6
+ metadata.gz: d00a35e7f8e172ba10759dae5a62c6eee46f5519f54e66295dbfa51a0958c55b40edc3d2fb9583b91562a4de51a646d1f87b3191ae7a5ac320f77b8fb7426cd4
7
+ data.tar.gz: cbcd6327de0c9b9708d4cd1e75f383515f2bd84c5aaab6263df43afb890330aead57333f5627a7f40d020389890010eaccf88eda59d41feb3c132e90cc9c9b97
data/README.md CHANGED
@@ -1,28 +1,42 @@
1
1
  # Git::Fancy
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Git fancy is a command line tool that makes switching between git branches faster and easier.
4
+ It also allows you to annotate branches and quickly match branches based on either part of the branch name or notes.
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/git/fancy`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+ Git fancy tracks branches by most recently used.
6
7
 
7
- ## Installation
8
+ ![Kapture 2025-05-06 at 02 31 07](https://github.com/user-attachments/assets/238e45c0-e612-4d58-8bd4-543356f52730)
8
9
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
10
 
11
- Install the gem and add to the application's Gemfile by executing:
11
+ ## Installation
12
12
 
13
- ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ```console
14
+ $ gem install git-fancy
15
15
  ```
16
16
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
17
+ ## Usage
18
18
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ The `sb` command is an alias for `switch-branch`. Both commands can be used interchangeably.
20
+
21
+ To list recently used branches: (sb only tracks recently used branches when you use sb to switch branches)
22
+ ```console
23
+ sb
21
24
  ```
22
25
 
23
- ## Usage
26
+ To switch to a branch when you only have a portion of a branch name:
27
+ ```console
28
+ sb <portion_of_branch_name>
29
+ ```
30
+
31
+ To annotate the current branch you are on:
32
+ ```console
33
+ sb -n "make the logo bigger"
34
+ ```
24
35
 
25
- TODO: Write usage instructions here
36
+ List all of the branches git fancy is tracking:
37
+ ```console
38
+ sb -l
39
+ ```
26
40
 
27
41
  ## Development
28
42
 
data/exe/sb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  # This is just an alias for the switch-branch script
4
- path = File.join(__dir__, './switch-branch')
4
+ path = File.join(__dir__, 'switch-branch')
5
5
  eval(File.read(path)) if File.exist?(path)
data/exe/switch-branch CHANGED
@@ -4,10 +4,13 @@
4
4
  # Switch to git branch by matching part of a name
5
5
 
6
6
  require 'optparse'
7
+ require 'pastel'
7
8
  require 'tty-prompt'
8
9
  require 'tty-logger'
9
10
  require 'sqlite3'
10
11
  require 'git/fancy'
12
+ require 'open3'
13
+ require 'debug'
11
14
 
12
15
  options = {}
13
16
 
@@ -27,12 +30,31 @@ OptionParser.new do |opts|
27
30
  end
28
31
  end.parse!
29
32
 
33
+ logger = TTY::Logger.new do |config|
34
+ config.handlers = [[:console, {
35
+ styles: {
36
+ info: {
37
+ color: :green,
38
+ symbol: '→',
39
+ label: nil,
40
+ levelpad: 0,
41
+ },
42
+ warn: {
43
+ label: nil,
44
+ },
45
+ error: {
46
+ label: nil,
47
+ },
48
+ }
49
+ }]]
50
+ end
51
+ @logger = logger
52
+
30
53
  Signal.trap('TERM') do
31
- puts 'Terminating...'
54
+ @logger.error 'Terminating...'
32
55
  shutdown
33
56
  end
34
57
 
35
- logger = TTY::Logger.new
36
58
 
37
59
  @search_term = ARGV[0]&.strip
38
60
  search_term = @search_term
@@ -48,7 +70,7 @@ end
48
70
 
49
71
  if options[:delete_history]
50
72
  @branch_history.purge_history!
51
- logger.success @color.green('Removed branch history')
73
+ @logger.success @color.green('Removed branch history')
52
74
  exit 0
53
75
  end
54
76
 
@@ -62,22 +84,22 @@ end
62
84
  if options[:note]
63
85
  @branch_history.add_branch @branch_history.current_branch
64
86
  @branch_history.add_note options[:note]
65
- logger.success @color.green('Note added')
87
+ @logger.success @color.green('Note added')
66
88
  exit 0
67
89
  end
68
90
 
69
91
  if options[:list]
70
92
  branches = @branch_history.recent_branches(limit: 20, include_current: true)
71
93
  if branches.empty?
72
- logger.warn 'No recent branches found'
94
+ @logger.warn 'No recent branches found'
73
95
  exit 0
74
96
  end
75
97
 
76
- puts 'Recent branches:'
98
+ @logger.info 'Recent branches:'
77
99
  branches.each do |branch|
78
- time = branch.last_accessed ? Time.at(branch.last_accessed).strftime('%y-%m-%d') : nil
100
+ time = branch.last_accessed ? Time.at(branch.last_accessed).strftime('%Y-%m-%d') : nil
79
101
  note = branch.note ? @color.yellow(branch.note) : nil
80
- puts [time, branch.name, note].compact.join(' -- ')
102
+ puts [time, branch.name, note].compact.join(@color.dim(' '))
81
103
  end
82
104
  exit 0
83
105
  end
@@ -99,32 +121,35 @@ end
99
121
  branches = find_branches(search_term) || []
100
122
 
101
123
  def switch_branch(branch_name, location:)
102
- logger = TTY::Logger.new
103
- `git switch #{branch_name}`
124
+ Open3.popen3("git switch #{branch_name}") do |stdin, stdout, stderr, wait_thr|
125
+ # For some reason, even succesful git switch goes to stderr
126
+ out = stderr.read
127
+ if wait_thr.value.success?
128
+ @logger.success @color.green(out.strip)
129
+ else
130
+ # Also for some reason errors are duplicated
131
+ @logger.error @color.red(out.split("\nerror: ").first.strip)
132
+ exit 1
133
+ end
134
+ end
104
135
 
105
136
  @branch_history.add_branch(branch_name)
106
137
  branch_note = @branch_history.branches_matching_notes(@search_term).find { |b| b.name == branch_name }&.note if @search_term
107
- logger.success "Switched to #{location} branch: #{branch_name} #{branch_note ? @color.yellow(branch_note) : ''}"
108
138
  end
109
139
 
110
140
  def select_branch(branches, location: 'local', selected_branch: nil)
111
- logger = TTY::Logger.new
112
- prompt = TTY::Prompt.new
113
-
114
141
  choices = branches.each_with_object({}) do |branch, hash|
115
142
  time = branch.last_accessed ? Time.at(branch.last_accessed).strftime('%y-%m-%d') : nil
116
143
  note = branch.note ? @color.yellow(branch.note) : nil
117
144
  key = [time, branch.name, note].compact.join(' -- ')
118
145
  hash[key] = branch.name
119
146
  end
120
-
121
- selection = prompt.select("Choose #{location} branch", choices.merge('none' => 'none'))
122
- if selection == 'none'
123
- logger.info 'Cancelling selection'
124
- exit 0
125
- end
126
-
127
- switch_branch(selection, location:)
147
+ prompt = TTY::Prompt.new
148
+ cancel_str = @color.dim('ctrl-c to cancel')
149
+ selection = prompt.select("Choose #{location} branch #{cancel_str}", choices)
150
+ switch_branch(selection, location:) if selection != 'none'
151
+ rescue TTY::Reader::InputInterrupt
152
+ exit 0
128
153
  end
129
154
 
130
155
  if branches.length == 1
@@ -135,10 +160,10 @@ if branches.length == 1
135
160
 
136
161
  switch_branch(branch_name, location: 'local')
137
162
  elsif branches.length > 1
138
- logger.warn 'More than one branch name matched' unless search_term
163
+ @logger.warn 'More than one branch name matched' if search_term
139
164
  select_branch(branches, location: 'local')
140
165
  else
141
- logger.warn "No local branches named: '#{branches.join(', ')}' with that name were found"
166
+ @logger.warn "No local branches named: '#{branches.join(', ')}' with that name were found"
142
167
  remote_branches = prep_branch_results(`git branch -r | grep #{search_term} | cut -f 1`)
143
168
 
144
169
  select_branch(remote_branches, location: 'remote')
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Git
4
4
  module Fancy
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-fancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean McCleary