Skip to content

Commit db775a6

Browse files
authored
Merge pull request #1 from kompiro/add_add_command
Add AddCommand
2 parents 660655f + e485e0f commit db775a6

File tree

10 files changed

+251
-102
lines changed

10 files changed

+251
-102
lines changed

lib/git_topic/cli.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
require 'open3'
55

66
require 'git_topic/version'
7-
require 'git_topic/commands/list'
7+
require 'git_topic/commands/add'
88
require 'git_topic/commands/edit'
9+
require 'git_topic/commands/list'
910
require 'git_topic/commands/show'
1011

1112
module GitTopic
@@ -15,14 +16,15 @@ class Cli < Thor
1516

1617
desc 'list', 'Show managed topics'
1718
option :version, aliases: 'v'
19+
option :all, aliases: 'a'
1820
def list
1921
# Show version if -v specified
2022
if options[:version]
2123
version if options[:version]
2224
return
2325
end
2426

25-
command = GitTopic::Commands::List.new
27+
command = GitTopic::Commands::List.new options
2628
command.execute
2729
end
2830

@@ -43,10 +45,10 @@ def version
4345
puts GitTopic::VERSION
4446
end
4547

46-
desc 'add topic_name', 'Remember topic'
47-
def add(topic_name)
48-
puts "add #{topic_name}"
49-
raise 'not implemented'
48+
desc 'add topic_name summary', 'Remember topic'
49+
def add(topic_name, summary)
50+
command = GitTopic::Commands::Add.new topic_name, summary
51+
command.execute
5052
end
5153

5254
desc 'start topic_name', 'Transfer topic_name to branch to implement code'

lib/git_topic/commands/add.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module GitTopic
4+
module Commands
5+
# add command register topic summary
6+
class Add
7+
def initialize(topic_name, summary)
8+
@topic_name = topic_name
9+
@summary = summary
10+
end
11+
12+
def execute
13+
system("git config --add topic.#{@topic_name} #{@summary}")
14+
end
15+
end
16+
end
17+
end

lib/git_topic/commands/list.rb

+12-84
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,24 @@
11
# frozen_string_literal: true
22

3-
require 'term/ansicolor'
3+
require 'git_topic/formatter/branches'
4+
require 'git_topic/formatter/topics'
45

56
module GitTopic
67
module Commands
78
# list command shows summarized topic information
89
class List
9-
include Term::ANSIColor
10-
def execute
11-
branches, current_branch = parse_branches
12-
print_header(branches.first)
13-
print_contents(branches, current_branch)
14-
end
15-
16-
private
17-
18-
Branch = Struct.new('Branch', :name, :rev)
19-
20-
def print_header(branch)
21-
rev_length = branch.rev.length
22-
header_format = " %-20s %-#{rev_length}s %s"
23-
puts format(header_format, :Branch, :Rev, :Summary)
24-
puts '-' * 80
10+
def initialize(options = {})
11+
@options = options
12+
@all = options[:all]
2513
end
2614

27-
def print_contents(branches, current_branch)
28-
branches.each do |branch|
29-
print_line(current_branch, branch)
30-
end
31-
end
32-
33-
def parse_branches
34-
branches = []
35-
current_branch = nil
36-
_stdin, stdout, _stderr, _wait_thr = *Open3.popen3('git branch -v')
37-
stdout.each do |line|
38-
branch_name, rev, current_candidate = parse_branch(line)
39-
current_branch ||= current_candidate
40-
branches << Branch.new(branch_name, rev)
41-
end
42-
[branches, current_branch]
43-
end
44-
45-
BRANCH_FORMAT = /
46-
\s*(?<current_exp>\*\ )?
47-
(?<branch_name>\S+)\s+
48-
(?<rev>\S+)\s+(.*)
49-
/x
50-
51-
def parse_branch(line)
52-
matched = line.match(BRANCH_FORMAT)
53-
raise 'cannot parse branch' unless matched
54-
branch_name = matched[:branch_name]
55-
rev = matched[:rev]
56-
current_branch = matched[:current_exp] ? branch_name : nil
57-
[branch_name, rev, current_branch]
58-
end
59-
60-
def print_line(current_branch, branch)
61-
branch_name = branch.name
62-
rev = branch.rev
63-
description = get_description_of branch
64-
return if description.nil?
65-
branch_format = branch_format(branch_name, current_branch)
66-
truncated_name = truncate(branch_name)
67-
puts format("#{branch_format} %s %s", truncated_name, rev, description)
68-
end
69-
70-
def get_description_of(branch)
71-
config_key = "branch.#{branch.name}.description"
72-
command = "git config #{config_key}"
73-
_stdin, stdout, _stderr, _wait_thr = *Open3.popen3(command)
74-
return nil if stdout.eof?
75-
stdout.readline
76-
end
77-
78-
def branch_format(branch_name, current_branch)
79-
if branch_name == current_branch
80-
"* #{green}#{bold}%-20s#{clear}"
81-
else
82-
" #{bold}%-20s#{clear}"
83-
end
84-
end
85-
86-
def truncate(str, truncate_at: 20)
87-
omission = '...'
88-
length_with_room_for_omission = truncate_at - omission.length
89-
if str.length > truncate_at
90-
"#{str[0, length_with_room_for_omission]}#{omission}"
91-
else
92-
str
93-
end
15+
def execute
16+
branches = ::GitTopic::Formatter::Branches.new @options
17+
branches.print
18+
return unless @all
19+
puts ''
20+
topics = ::GitTopic::Formatter::Topics.new
21+
topics.print
9422
end
9523
end
9624
end

lib/git_topic/formatter/branches.rb

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# frozen_string_literal: true
2+
3+
require 'git_topic/formatter/helper'
4+
require 'term/ansicolor'
5+
6+
module GitTopic
7+
module Formatter
8+
# summarizes branches information
9+
class Branches
10+
include Term::ANSIColor
11+
include GitTopic::Formatter::Helper
12+
13+
def initialize(options)
14+
@all = options[:all]
15+
end
16+
Branch = Struct.new('Branch', :name, :rev)
17+
18+
def print
19+
puts "#{bold}[Branches]#{clear}" if @all
20+
branches, current_branch = parse_branches
21+
print_header(branches.first)
22+
print_contents(branches, current_branch)
23+
end
24+
25+
def print_header(branch)
26+
rev_length = branch.rev.length
27+
header_format = " %-20s %-#{rev_length}s %s"
28+
puts format(header_format, :Branch, :Rev, :Summary)
29+
puts '-' * 80
30+
end
31+
32+
def print_contents(branches, current_branch)
33+
branches.each do |branch|
34+
print_line(current_branch, branch)
35+
end
36+
end
37+
38+
def parse_branches
39+
branches = []
40+
current_branch = nil
41+
_stdin, stdout, _stderr, _wait_thr = *Open3.popen3('git branch -v')
42+
stdout.each do |line|
43+
branch_name, rev, current_candidate = parse_branch(line)
44+
current_branch ||= current_candidate
45+
branches << Branch.new(branch_name, rev)
46+
end
47+
[branches, current_branch]
48+
end
49+
50+
BRANCH_FORMAT = /
51+
\s*(?<current_exp>\*\ )?
52+
(?<branch_name>\S+)\s+
53+
(?<rev>\S+)\s+(.*)
54+
/x
55+
56+
def parse_branch(line)
57+
matched = line.match(BRANCH_FORMAT)
58+
raise 'cannot parse branch' unless matched
59+
branch_name = matched[:branch_name]
60+
rev = matched[:rev]
61+
current_branch = matched[:current_exp] ? branch_name : nil
62+
[branch_name, rev, current_branch]
63+
end
64+
65+
def print_line(current_branch, branch)
66+
branch_name = branch.name
67+
rev = branch.rev
68+
description = get_description_of branch
69+
return if description.nil?
70+
branch_format = branch_format(branch_name, current_branch)
71+
truncated_name = truncate(branch_name)
72+
puts format("#{branch_format} %s %s", truncated_name, rev, description)
73+
end
74+
75+
def get_description_of(branch)
76+
config_key = "branch.#{branch.name}.description"
77+
command = "git config #{config_key}"
78+
_stdin, stdout, _stderr, _wait_thr = *Open3.popen3(command)
79+
return nil if stdout.eof?
80+
stdout.readline
81+
end
82+
83+
def branch_format(branch_name, current_branch)
84+
if branch_name == current_branch
85+
"* #{green}#{bold}%-20s#{clear}"
86+
else
87+
" #{bold}%-20s#{clear}"
88+
end
89+
end
90+
end
91+
end
92+
end

lib/git_topic/formatter/helper.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module GitTopic
4+
module Formatter
5+
# Helper class for Formatter classes
6+
module Helper
7+
def truncate(str, truncate_at: 20)
8+
omission = '...'
9+
length_with_room_for_omission = truncate_at - omission.length
10+
if str.length > truncate_at
11+
"#{str[0, length_with_room_for_omission]}#{omission}"
12+
else
13+
str
14+
end
15+
end
16+
end
17+
end
18+
end

lib/git_topic/formatter/topics.rb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
require 'term/ansicolor'
4+
5+
module GitTopic
6+
module Formatter
7+
# summarizes topics information
8+
# Topic means theme that not start to implement
9+
class Topics
10+
include Term::ANSIColor
11+
include GitTopic::Formatter::Helper
12+
13+
def print
14+
puts "#{bold}[Topics]#{clear}"
15+
topics = parse_topics
16+
print_header
17+
print_contents topics
18+
end
19+
20+
private
21+
22+
def print_header
23+
header_format = ' %-20s %s'
24+
puts format(header_format, :Topic, :Summary)
25+
puts '-' * 80
26+
end
27+
28+
Topic = Struct.new('Topic', :name, :summary)
29+
LIST_TOPIC_COMMAND = 'git config --get-regexp ^topic.\*'
30+
31+
def parse_topics
32+
topics = []
33+
_stdin, stdout, _stderr, _wait_thr = *Open3.popen3(LIST_TOPIC_COMMAND)
34+
stdout.each do |line|
35+
name, summary = parse_topic(line)
36+
topics << Topic.new(name, summary)
37+
end
38+
topics
39+
end
40+
41+
def parse_topic(line)
42+
matched = line.match(/topic\.(?<topic_name>\S+)\s+(?<summary>.*)/)
43+
raise 'cannot parse topic' unless matched
44+
topic_name = matched[:topic_name]
45+
summary = matched[:summary]
46+
[topic_name, summary]
47+
end
48+
49+
def print_contents(topics)
50+
topics.each do |topic|
51+
print_line topic
52+
end
53+
end
54+
55+
def print_line(topic)
56+
truncated_name = truncate(topic.name)
57+
summary = topic.summary
58+
puts format(" #{bold}%-20s#{clear} %s", truncated_name, summary)
59+
end
60+
end
61+
end
62+
end

lib/git_topic/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module GitTopic
4-
VERSION = '0.2.5'
4+
VERSION = '0.3.0'
55
end

spec/git_topic/commands/list_spec.rb

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
RSpec.describe GitTopic::Commands::List do
66
subject(:command) { described_class.new }
77

8-
it { is_expected.to be_truthy }
9-
108
describe '#execute' do
119
def setup_git_branch(output)
12-
stdout = StringIO.new(output)
13-
allow(Open3).to receive(:popen3)
14-
.with('git branch -v').and_return([nil, stdout, nil, nil])
10+
setup_command('git branch -v', output)
1511
end
1612

1713
def setup_git_config
@@ -20,12 +16,7 @@ def setup_git_config
2016
end
2117

2218
def setup_branch_description(branch_name, description)
23-
stdout = instance_double(IO)
24-
allow(stdout).to receive(:readline).and_return(description)
25-
allow(stdout).to receive(:eof?).and_return(false)
26-
allow(Open3).to receive(:popen3)
27-
.with("git config branch.#{branch_name}.description")
28-
.and_return([nil, stdout, nil, nil])
19+
setup_command("git config branch.#{branch_name}.description", description)
2920
end
3021

3122
context 'rev length is 7' do

0 commit comments

Comments
 (0)