Sha256: 2158909217ced7ed5d45251189b948e6f94d5c691ba38e18f6a1c26d96135680

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

#!/usr/bin/env ruby
#
# git-select #story-id
#
#   Looks up your current branches for the first occurence of
#   the story-id and checkout it
#
require 'rubygems'
require 'term/ansicolor'

# Helper functions
def git_dir?
  `git config --get remote.origin.url`.strip == ''
end

def process(branches)
  branches.split("\n").map { |branch| branch.strip.gsub(%r(\* ), '')}
end

def select(branches, story_id)
  branches.select { |branch| branch =~ /#{story_id}/ }
end


if git_dir?
  puts "Not a valid git repo"
  exit 1
end

if ARGV[0] && ARGV[0] =~ /^\d+$/
  story_id = ARGV[0]
else
  puts "story-id is not a number"
  exit 1
end


# Find locally first
locals = process(`git branch`)

# Select matching locals
found  = select(locals, story_id)

case found.size
when 0
  puts Term::ANSIColor.yellow 'Failed lookup on local branches'
when 1
  `git checkout #{found.first}`
  exit 0
else
  puts "Found #{found.size} matches on locals:"
  found.each do |branch|
    puts "  #{branch}"
  end
  exit 1
end

# Find remote branch
remotes = process(`git branch -a`)

# Select matching remotes
found   = select(remotes, story_id)

case found.size
when 0
  puts Term::ANSIColor.yellow 'Failed lookup on remote branches'
else
  puts "Found #{found.size} matches on remotes:"
  found.each do |branch|
    puts "  #{branch.gsub(%r(^remotes/origin/), "")}"
  end
  exit 1
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git-whistles-0.6.1 bin/git-select