Skip to content

Instantly share code, notes, and snippets.

@natesholland
Last active March 15, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natesholland/dccc1cd42787a72c863d to your computer and use it in GitHub Desktop.
Save natesholland/dccc1cd42787a72c863d to your computer and use it in GitHub Desktop.
Tagging Script
#!/usr/bin/ruby
BRANCH_REGEX = /(hotfix|patch|release)\/\d{4}_\d{2}_\d{2}/
def check_input(string, branch_name)
while true
puts string
input = gets
if input.start_with?('y') || input.start_with?('Y')
# the user wants to continue, exit loop.
break
elsif input.start_with?('n') || input.start_with?('N')
# something went wrong, abort and exit out
`git checkout #{branch_name}`
puts "Aborting now."
exit 1
else
# unexpected input
puts "I didn't understand, please enter 'Y' or 'N'."
end
end
end
branch_name = `git rev-parse --abbrev-ref HEAD`.strip
last_commit = `git log --pretty=format:'%H' -n 1`
unless BRANCH_REGEX.match(branch_name)
puts "Aborting now: branch name is not a hotfix|patch|release"
exit 1
end
check_input("We are going to tag this sha: #{last_commit} is this correct? Y|N", branch_name)
tag_name = BRANCH_REGEX.match(branch_name)[0] + "_#{Time.now.hour.to_s.rjust(2, '0')}#{Time.now.min.to_s.rjust(2, '0')}"
# checkout master
`git checkout master`
# pulling the latest code from master
`git pull`
last_master_sha = `git log --pretty=format:'%H' -n 1`
# ensure that the head of master is contained in what we are tagging
branches_containing_master = `git branch --contains #{last_master_sha}`
branches_containing_master = branches_containing_master.split("\n").map(&:strip)
unless branches_containing_master.include? branch_name
`git checkout #{branch_name}` # ensure that we switch back to the original branch
puts "Aborting now: branch does not contain the head of master."
exit 1
end
check_input("We are about to make tag: #{tag_name} is this correct? Y|N", branch_name)
`git checkout #{branch_name}`
puts "Making tag: #{tag_name}"
`git tag #{tag_name}`
puts "Pushing #{tag_name} up to origin."
`git push origin #{tag_name}`
@natesholland
Copy link
Author

A script I am writing to make tagging for releases more automated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment