Skip to content

Instantly share code, notes, and snippets.

@yjukaku
Created March 19, 2020 16:16
Show Gist options
  • Save yjukaku/bd9e7289ee88c4d7045c47bbcc9aefa8 to your computer and use it in GitHub Desktop.
Save yjukaku/bd9e7289ee88c4d7045c47bbcc9aefa8 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
def prompt(message)
STDOUT.write "#{message}: "
STDIN.gets.strip
end
def error_and_exit!(error)
$stderr.puts error
exit(1)
end
def run
issue_desc = ARGV[0]&.strip
while issue_desc.nil?
issue_desc = prompt "Enter ticket number or link"
end
number =
if issue_desc.to_i.to_s == issue_desc
issue_desc
else
matches = issue_desc.match(/QWEB-(\d+)/)
if matches.nil?
error_and_exit! "Couldn't parse issue number from '#{issue_desc}'"
end
matches[1]
end
puts "Searching for branch for #{number}..."
issue = "qweb-" + number
matching_branches = `git branch --list #{issue}*`.strip.split("\n")
matching_branches = matching_branches.map { |branch_name| branch_name.gsub("*", "").strip }
if matching_branches.count == 0
puts "No matching branch. Checking out new one based on master..."
checkout_result = system("git checkout master && git pull origin master")
if !checkout_result
error_and_exit! "Couldn't checkout and pull master."
end
branch_suffix = prompt "Please enter a descriptive suffix for the branch name (eg. 'do-something')"
system("git checkout -b #{issue}-#{branch_suffix}")
exit(0)
else
branch_name = nil
loop do
puts matching_branches.each_with_index.map { |branch_name, i| "\t#{i+1}. #{branch_name}" }.join("\n")
branch_choice = prompt("Choose a branch to checkout").to_i
branch_name = matching_branches[branch_choice - 1]
if (1..matching_branches.length).cover?(branch_choice)
system("git checkout #{branch_name}")
exit(0)
end
end
end
end
begin
run
rescue Interrupt
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment