#!/usr/bin/env ruby # encoding: UTF-8 # # git-explore [-r ref] [-p path] [-l line[-line]] # # Explore the given reference on the remote origin website # require 'rubygems' require 'optparse' require 'term/ansicolor' require 'git-whistles/app' class App < Git::Whistles::App GITHUB_URL = 'https://github.com' def main(args) super parse_args!(args) @origin = run!("git config --get remote.origin.url").strip unless github? die 'Unknown origin. Only github supported at the moment', usage: true end # This has to support both variants # https://github.com/mezis/git-whistles.git # git@github.com:mezis/git-whistles.git unless @origin.match /github\.com[:\/](.+)\.git/ die "Error parsing #{@origin} could not find repo" end repo = $1 path = options.path ? "/#{ options.path.strip }" : nil reference = path ? "/blob/#{ options.ref.strip }" : "/tree/#{ options.ref.strip }" lines = path && options.lines ? "##{options.lines.split('-').map{ |line| "L#{line}" }.join('-')}" : '' url = "#{GITHUB_URL}/#{repo}#{reference}#{path}#{lines}" puts "opening #{ url }..." run! "open #{ url }" end def defaults { ref: run!('git rev-parse --abbrev-ref HEAD'), file: nil } end def option_parser @option_parser ||= OptionParser.new do |op| op.banner = "Usage: git explore [-r ref] [-p path] [-l line[-line]]" op.on('-r', '--ref REFERENCE', 'Reference to explore. Defaults to current branch') do |ref| options.ref = ref end op.on('-p', '--path PATH', 'Path to explore. Defaults to /') do |path| options.path = path end op.on('-l', '--line LINE', 'Line(s) to highlight. Examples: "2", "10-15". Defaults to nil') do |lines| options.lines = lines end op.on_tail('-h', '--help', 'Show this message') do puts op exit end end end private def github? @origin.match %r{github.com} end end App.run!