-
-
Save dux/6850e572b3bce1f9dab8419ec6e61a0c to your computer and use it in GitHub Desktop.
git helper, pull before push, nice logs, easy commits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'colorize' | |
@actions = {} | |
@branch = `git rev-parse --abbrev-ref HEAD`.to_s.chomp.sub(/\*\s/,'') | |
def cputs(data) | |
puts data.split("\n").map{ |el| " #{el}" }.join("\n") | |
end | |
def die(data) | |
puts data.red | |
exit | |
end | |
def action(name, desc, &proc) | |
@actions[name] = { desc:desc, proc: proc } | |
end | |
def run(command) | |
puts command.blue | |
system "#{command} 2>&1" | |
end | |
def run!(command) | |
puts command.blue | |
data = `#{command} 2>&1` | |
puts data if data | |
data | |
end | |
def get(command) | |
puts command.yellow | |
data = `#{command}` | |
puts data | |
data | |
end | |
def show_help | |
puts "Command not defined. Usage: g [command] [option]" | |
for k,v in @actions.sort | |
puts "#{k.to_s.rjust(5)} - #{v[:desc]}" | |
end | |
exit | |
end | |
unless Dir.exists?('.git') | |
puts 'No .git directory'.red | |
exit | |
end | |
### | |
action :c, 'git and . and commit' do | |
system 'git add .' | |
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 ./app -name '*.orig' -o -name '*_LOCAL_*' -o -name '*_BACKUP_*' -o -name '*_BASE_*' -o -name '*_REMOTE_*' | grep -v '/tmp/'`.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 | |
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 | |
commit_message.gsub!("'","'\\''") | |
commit_message.capitalize! | |
run "git commit -m '#{ticket_name}: #{commit_message}'" | |
end | |
action :am, 'ammend - append add to last commit' do | |
run 'git add .' | |
run 'git commit --ammend' | |
end | |
action :push, 'git push origin [current branch]' do | |
# define default upstrem | |
`git branch -u origin/#{@branch}` | |
run!("git pull origin #{@branch}") | |
run "git push origin #{@branch}" | |
end | |
action :s, 'git status' do | |
run 'git status' | |
end | |
action :p, 'git pull origin [current branch]' do | |
run "git pull origin #{@branch}" | |
end | |
action :u, 'undo add: git rest --mixed' do | |
run 'git reset --mixed' | |
end | |
action :uc, 'undo commit: git reset --soft HEAD^' do | |
run 'git reset --soft HEAD~' | |
end | |
action :l, 'git "fancy" log' do | |
run "git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" | |
end | |
action :ll, 'my latest log entries' do | |
run 'git log --pretty=format:"%h%x09%an%x09%ad%x09%s" | grep Dino | more' | |
end | |
action :rm, 'remove file or dir from git' do | |
name = ARGV[1] | |
run "git rm -r --cached #{name}" | |
run "git add ." | |
run "git update-index --assume-unchanged #{name}" if name =~ /\.\w+$/ | |
end | |
action :b, 'change branch' do | |
branches = `git branch`.split("\n") | |
name = ARGV[1] | |
if name | |
branches.map!{ |el| el.sub!(/[\s\*]+/,'') } | |
for branch in branches | |
if branch[0, name.length] == name | |
data = get "git checkout #{branch}" | |
run "git pull origin #{branch}" | |
exit | |
end | |
end | |
else | |
puts branches | |
end | |
end | |
action :bk, 'backup _LOCAL_ and _BACKUP_ to tmp folder' do | |
`mkdir tmp` | |
for el in ['LOCAL', 'BACKUP', 'BASE', 'REMOTE'] | |
run "find . -name *_#{el}_* -type f -exec /bin/mv {} ./tmp \\;" | |
end | |
run "find . -name *.orig -type f -exec /bin/mv {} ./tmp \\;" | |
end | |
action :re, 'restore single file to last checkout' do | |
die 'No file defined' unless ARGV[1] | |
run "rm #{ARGV[1]}" | |
run "git reset HEAD #{ARGV[1]}" | |
run "git checkout -- #{ARGV[1]}" | |
end | |
action :rs, 'reset HEAD to remote' do | |
run 'git fetch origin' | |
run 'git reset --hard origin/HEAD' | |
end | |
action :h, 'gitk - show file history' do | |
run "gitk #{ARGV[1]}" | |
end | |
### | |
@command = ARGV[0] ? ARGV[0].to_sym : nil | |
show_help unless @command | |
if @actions[@command] | |
@actions[@command][:proc].call | |
else | |
puts "Command #{@command.to_s.red} not found" | |
show_help | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment