-
Notifications
You must be signed in to change notification settings - Fork 31
/
hitch.rb
150 lines (120 loc) · 2.97 KB
/
hitch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'highline'
require 'yaml'
require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib hitch author]))
require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib hitch ui]))
module Hitch
VERSION = '1.1.0'
def self.print_info
if Hitch.pairing? && STDOUT.tty?
Hitch::UI.highline.say("#{Hitch.git_author_name} <#{Hitch.git_author_email}>")
end
end
def self.export(pairs)
Hitch.current_pair = pairs
write_export_file
print_info
end
def self.expire_command(time)
%Q(sleep #{to_seconds(time)} && #{Hitch.bin_path} --unhitch&)
end
def self.unhitch
Hitch.current_pair = []
write_export_file
end
def self.author_command
if Hitch.pairing?
%Q{export GIT_AUTHOR_NAME="#{Hitch.git_author_name}" GIT_AUTHOR_EMAIL="#{Hitch.git_author_email}"}
else
"unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL"
end
end
def self.group_email
config[:group_email] ||= Hitch::UI.prompt_for_group_email
end
def self.group_email=(email)
config[:group_email] = email
write_file
end
def self.current_pair
config[:current_pair] ||= []
end
def self.current_pair=(pairs)
config[:current_pair] = []
pairs.each do |author|
if Hitch::Author.find(author)
config[:current_pair] << author
else
if Hitch::UI.prompt_for_pair(author)
config[:current_pair] << author
end
end
end
write_file
end
def self.git_author_name
devs = current_pair.sort.map {|pair| Hitch::Author.find(pair)}
case devs.length
when 1, 2
devs.join(" and ")
else
"#{devs[0...-1].join(', ')}, and #{devs[-1]}"
end
end
def self.git_author_email
"#{group_prefix}+#{current_pair.sort.join('+')}@#{group_domain}"
end
def self.group_prefix
group_email.split('@').first
end
def self.group_domain
group_email.split('@').last
end
def self.setup_path
File.join(File.dirname(__FILE__), 'hitch', 'hitch.sh')
end
def self.print_setup_path
puts setup_path
end
def self.setup
Hitch::UI.highline.say(File.read(setup_path))
end
def self.write_file
File.open(hitchrc, File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
YAML.dump(config, out)
end
end
private
def self.config
@config ||= get_config
end
def self.get_config
if File.exists?(hitchrc)
yamlized = YAML::load_file(hitchrc)
return yamlized if yamlized.kind_of?(Hash)
end
return {}
end
def self.hitchrc
File.expand_path('~/.hitchrc')
end
def self.hitch_export_authors
File.expand_path('~/.hitch_export_authors')
end
def self.bin_path
%x[which hitch].chomp
end
def self.pairing?
current_pair.any?
end
def self.to_seconds(value)
value = value.to_s.gsub(/[^\d]/, '').to_i
unless value.zero?
value * 60 * 60
else
raise StandardError
end
end
def self.write_export_file
File.open(hitch_export_authors, 'w'){|f| f.write(author_command) }
end
end