Module: GitUI
Defined Under Namespace
Classes: Action
Instance Method Summary collapse
- #act(*a, &b) ⇒ Object
- #add_untracked ⇒ Object
- #changes ⇒ Object
- #commit(*cs) ⇒ Object
- #compose(*lambdas) ⇒ Object
- #cook_stty! ⇒ Object
- #decide(prompt, acts, color = :default) ⇒ Object
- #determine_cmd(args) ⇒ Object
- #edit(filename) ⇒ Object
- #editor ⇒ Object
- #find_dotgit(path = '.') ⇒ Object
- #fsz(fn) ⇒ Object
- #pager ⇒ Object
- #record(*args) ⇒ Object
- #uncook_stty! ⇒ Object
- #untracked_files ⇒ Object
Instance Method Details
#act(*a, &b) ⇒ Object
14 15 16 |
# File 'lib/git_ui.rb', line 14 def act *a, &b Action.new(*a, &b) end |
#add_untracked ⇒ Object
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 |
# File 'lib/git_ui.rb', line 63 def add_untracked cs = untracked_files color = :yellow if cs.empty? puts "No new files; try your .gitignore, maybe?".colorize(color) return [] end i = 0 acts = [ act('y', "Yes, add this file.") { cs[i][:action] = :add; i += 1 }, act('n', 'No, don\'t add this file.') { cs[i][:action] = nil; i += 1 }, act('v', 'View the file') { print File.read(cs[i][:filename]) }, act('p', "View this file in a pager (#{pager})") { system "#{pager} #{cs[i][:filename]}" }, act('e', "Edit this file using \"#{editor}\"") { edit cs[i][:filename] }, act('d', 'Done, skip to commit step.') { i = cs.size }, act('a', 'Add all remaining') { cs[i..-1].each { |c| c[:action] = :add } i = cs.size }, act('q', 'Quit, skip commit step.') { cs.clear }, act('j', 'Jump to next file in list') { i += 1 }, act('k', 'Go back to previous file in list') { i -= 1; i = 0 if i < 0 }, ] while i < cs.size puts "#{cs[i][:filename]} (#{fsz(cs[i][:filename])}, #{i + 1}/#{cs.size})" decide "Add?", acts, color end cs end |
#changes ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/git_ui.rb', line 47 def changes IO.popen('git diff --raw', 'r').readlines.grep(/^:/).map { |l| src_mode, dst_mode, src_sha1, dst_sha1, status, src_path, dst_path = l.sub(/^:/, '').split(/\s+/) { :status => status, :filename => (dst_path || src_path).chomp, } } end |
#commit(*cs) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/git_ui.rb', line 180 def commit(*cs) fs = cs.select { |c| [:commit, :add].include? c[:action] }.map { |c| c[:filename] } if fs.empty? puts "Nothing to commit.".colorize(:red) return end puts "Committing #{fs.join(', ')}...".colorize(:green) system 'git', 'add', *fs unless system('git', 'commit', *fs) # Put things where they were if the commit failed. puts "Commit failed! Resetting #{fs.join(', ')}.".colorize(:red) system 'git', 'reset', *fs end end |
#compose(*lambdas) ⇒ Object
18 19 20 21 22 |
# File 'lib/git_ui.rb', line 18 def compose *lambdas lambda { |*args| lambdas.reverse.inject(args) { |acc, f| f.call *acc } } end |
#cook_stty! ⇒ Object
223 224 225 |
# File 'lib/git_ui.rb', line 223 def cook_stty! system "stty sane" end |
#decide(prompt, acts, color = :default) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/git_ui.rb', line 102 def decide prompt, acts, color = :default keys = acts.map { |act| act.key } << '?' prompt = "#{prompt} [#{keys.join('')}]: ".colorize(color) act = nil loop { uncook_stty! print prompt $stdout.flush inp = $stdin.read(1) print "\n" if inp == '?' puts acts.map { |a| " #{a.key}: #{a.desc}" } elsif a = acts.find { |a| inp == a.key } cook_stty! return a.block.call else puts "No such action: #{inp}" end } end |
#determine_cmd(args) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/git_ui.rb', line 197 def determine_cmd args stty_cooked = lambda { |*args| system "stty sane"; args } stty_raw = lambda { |*args| system "stty -icanon min 1"; args } cmd, *args = args case cmd when /^rec/i lambda { compose(method(:commit), stty_cooked, method(:record), stty_raw)[*args] } when /^add/i lambda { compose(method(:commit), stty_cooked, method(:add_untracked), stty_raw)[*args] } else nil end end |
#edit(filename) ⇒ Object
126 127 128 |
# File 'lib/git_ui.rb', line 126 def edit filename system "#{editor} #{Escape.shell_single_word filename}" end |
#editor ⇒ Object
29 30 31 32 |
# File 'lib/git_ui.rb', line 29 def editor @editor ||= %w(GIT_UI_EDITOR GIT_EDITOR VISUAL EDITOR ).map { |e| ENV[e] }.compact.first || 'vi' end |
#find_dotgit(path = '.') ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/git_ui.rb', line 34 def find_dotgit(path = '.') path = original_path = File.(path) loop { return path if File.directory?(File.join(path, '.git')) p = File. File.join(path, '..') if p == path raise RuntimeError, "#{original_path} does not appear " \ "to be inside a git repository." end path = p } end |
#fsz(fn) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/git_ui.rb', line 167 def fsz fn sz = File.stat(fn).size rescue 0 st = sz.to_s us = %w(kB MB GB) u = 'B' while sz > 1024 && !us.empty? sz /= 1024.0 u = us.shift st = '%0.2f' % sz end st << u end |
#pager ⇒ Object
24 25 26 27 |
# File 'lib/git_ui.rb', line 24 def pager @pager ||= %w(GIT_UI_PAGER GIT_PAGER PAGER ).map { |e| ENV[e] }.compact.first || 'less' end |
#record(*args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/git_ui.rb', line 130 def record *args cs = changes if cs.empty? puts "No changes to record!".colorize(:cyan) return [] end i = 0 acts = [ act('y', "Yes, commit this change.") { cs[i][:action] = :commit; i += 1 }, act('n', 'No, don\'t commit this change.') { cs[i][:action] = nil; i += 1 }, act('v', 'View this patch') { system "git diff #{cs[i][:filename]}" }, act('p', "View this patch in a pager (#{pager})") { system "git diff #{cs[i][:filename]} | #{pager}" }, act('e', "Edit this file using \"#{editor}\"") { edit cs[i][:filename] }, act('d', 'Done, skip to commit step.') { i = cs.size }, act('a', 'Record all remaining') { cs[i..-1].each { |c| c[:action] = :record } i = cs.size }, act('q', 'Quit, skip commit step.') { cs.clear }, act('j', 'Jump to next patch in list') { i += 1 }, act('k', 'Go back to previous patch in list') { i -= 1; i = 0 if i < 0 }, ] while i < cs.size puts "#{cs[i][:filename]} #{cs[i][:status]} (#{fsz(cs[i][:filename])}, #{i + 1}/#{cs.size})" decide "Record?", acts, :cyan end cs end |
#uncook_stty! ⇒ Object
227 228 229 |
# File 'lib/git_ui.rb', line 227 def uncook_stty! system "stty -icanon min 1" end |
#untracked_files ⇒ Object
57 58 59 60 61 |
# File 'lib/git_ui.rb', line 57 def untracked_files IO.popen('git ls-files -o --exclude-standard', 'r').readlines.map { |f| { :filename => f.chomp } } end |