Skip to content

Instantly share code, notes, and snippets.

@janpipek
Last active August 29, 2015 14:05
Show Gist options
  • Save janpipek/453dddce52aa32ab4ed6 to your computer and use it in GitHub Desktop.
Save janpipek/453dddce52aa32ab4ed6 to your computer and use it in GitHub Desktop.
Check git status before doing something and auto-tag
def ask
print "Do you want to continue? (y/N) "
answer = STDIN.gets.strip.downcase
if answer != "y"
return false
end
return true
end
def get_branch
begin
return `git rev-parse --abbrev-ref HEAD`.strip
rescue
return nil
end
end
def get_status
begin
return `git status -s`.strip
rescue
return nil
end
end
def check_git( branch_name )
begin
branch = get_branch
if !branch
puts "Warning: git not found. Version checking disabled."
return true
elsif branch != branch_name
puts "Branch is not `#{branch_name}`, but `#{branch}`."
ask or return false
end
status = get_status
if status.length > 0
puts "There are uncommitted changes:"
system("git status -s")
ask or return false
end
rescue
end
return true
end
def create_tag( prefix )
status = get_status
if !status
puts "Warning: Cannot get git status, no tag will be created."
elsif status.length == 0
d = DateTime.now.strftime("%Y%m%d")
tag = "#{prefix}-#{d}"
puts "Adding git tag #{tag}..."
`git tag -f #{tag}`
else
puts "Uncommitted changes, no tag will be created."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment