Skip to content

Instantly share code, notes, and snippets.

@dux
Last active September 22, 2015 09:33
Show Gist options
  • Save dux/85d3490ca0a4be421152 to your computer and use it in GitHub Desktop.
Save dux/85d3490ca0a4be421152 to your computer and use it in GitHub Desktop.
script for git commit with preview of file changes, last 5 commits and enforcing of ticket prefix
#!/usr/bin/env ruby
require 'open3'
require 'colorize'
unless Dir.exists?('.git')
puts 'No .git directory'.red
exit
end
def run(what, silent=false)
puts what.green unless silent
stdin, stdout, stderr, wait_thread = Open3.popen3(what)
stderr.each_line do |line|
print line.red
end
stdout.each_line do |line|
print line unless silent
end
end
def cputs(data)
puts data.split("\n").map{ |el| " #{el}" }.join("\n")
end
run 'git add -A', true
status = `git status`.chomp
if status.index('nothing to commit')
puts status.yellow
exit
else
puts 'Modified files:'
cputs `git status`.split("\n").drop(4).map{ |el| el.sub(/^\t/,'') }.join("\n").yellow
puts '---'
puts 'Last 3 commits'
cputs `git log -3 --reverse --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit`
orig_files = `find . -name '*.orig'`.red
if orig_files.split("\n").length > 0
puts '---'
puts 'orig tmp git merge files'
cputs orig_files
end
puts '---'
end
# ticket_name = `git log -1 --pretty=%B`.split(':')[0]
# ticket_name = '' unless ticket_name.length < 15
branch = `git rev-parse --abbrev-ref HEAD`.to_s.chomp.sub(/\*\s/,'')
ticket_name = branch
print "Branch: #{branch}, ticket: #{ticket_name.red} / ENTER to type new branch name.\nMessage: "
commit_message = STDIN.gets.chomp
if commit_message.length == 0
print "Ticket name: "
ticket_name = STDIN.gets.chomp
if ticket_name.length == 0
puts 'Please enter new ticket name'.red
exit
else
print "Message: "
commit_message = STDIN.gets.chomp
end
end
ticket_name.gsub!(/[^\w\-_]/,'')
if commit_message == ''
puts 'Message is required'.red
exit
end
run "git commit -m '#{ticket_name}: #{commit_message}'"
chmod +x gc.rb
./gc.rb
no options required
gem install colorize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment