Skip to content

Commit

Permalink
Implement $ git-open
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanitoFatas committed Jul 18, 2016
1 parent 6b92bfb commit 887f909
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions git-open.gemspec
Expand Up @@ -18,4 +18,8 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = ["git-open"]
spec.require_paths = ["lib"]

spec.add_dependency "slop", "~> 4.0"
spec.add_dependency "git-remote-parser"
spec.add_dependency "launchy"
end
40 changes: 39 additions & 1 deletion lib/git/open.rb
Expand Up @@ -2,6 +2,44 @@

module Git
module Open
# Your code goes here...
class CLI
def self.execute
require "slop"
options = Slop.parse do |option|
option.on "-h".freeze, "--help".freeze, "Show this help".freeze
option.on "-v".freeze, "--version".freeze, "print the version".freeze do
puts_and_exit "git-open version #{Git::Open::VERSION}"
end
end

show_help(options) if options[:h] || options[:help] || ARGV.include?("help".freeze)

abort("Not a git repo".freeze) if `git rev-parse --is-inside-work-tree`.chomp! != "true".freeze

remotes = `git remote`.split("\n".freeze)

abort("No remote found".freeze) if remotes.empty?

url = if remotes.find { |remote| remote == "origin".freeze }
`git config --get remote.origin.url`.chomp!
else
`git config --get remote.#{remotes.first}.url`.chomp!
end

require "git/remote/parser"; require "launchy"
Launchy.open(open_url = Git::Remote::Parser.new.parse(url).html_url) do |exception|
puts "Attempted to open `#{open_url.to_s}' and failed because #{exception}"
end
end

def self.show_help(options)
puts_and_exit(options)
end

def self.puts_and_exit(message)
puts(message)
exit
end
end
end
end
2 changes: 1 addition & 1 deletion lib/git/open/version.rb
@@ -1,5 +1,5 @@
module Git
module Open
VERSION = "0.1.0"
VERSION = "1.0.0"
end
end
34 changes: 33 additions & 1 deletion spec/git/open_spec.rb
@@ -1,7 +1,39 @@
require "spec_helper"

describe Git::Open do
RSpec.describe Git::Open do
it "has a version number" do
expect(Git::Open::VERSION).not_to be nil
end
end

RSpec.describe Git::Open::CLI do
it "works for origin" do
expect(Launchy).to receive(:open).with("https://github.com/JuanitoFatas/git-open")

Git::Open::CLI.execute
end

it "fallback if origin not found" do
`git remote remove origin`
`git remote add upstream https://github.com/EvilJuanitoFatas/git-open.git`

expect(Launchy).to receive(:open).with("https://github.com/EvilJuanitoFatas/git-open")

Git::Open::CLI.execute

`git remote add origin git@github.com:JuanitoFatas/git-open.git`
`git remote remove upstream`
end

context "something went wrong with the Git::Remote::Parser" do
it "prints exception" do
expect(Git::Remote::Parser).to receive_message_chain(:new, :parse, :html_url) { nil }
failure_message = <<~FAILURE_MESSAGE
Attempted to open `' and failed because Failure in opening uri nil with options {}: undefined method `to_hash' for nil:NilClass
Did you mean? to_h
FAILURE_MESSAGE

expect { Git::Open::CLI.execute }.to output(failure_message).to_stdout
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -1,2 +1,4 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "git/open"
require "launchy"
require "git/remote/parser"

0 comments on commit 887f909

Please sign in to comment.