Commit 3073f31

mo khan <mo@mokhan.ca>
2021-03-19 22:14:41
extract repo class
1 parent 98eb064
lib/jive/cli.rb
@@ -9,8 +9,6 @@ require "jive"
 module Jive
   module Cli
     class App < Thor
-      SSH_REGEX = %r{\Agit@(?<host>.+):(?<account>.+)/(?<repo>.+)\z}.freeze
-
       package_name "jive"
 
       def self.exit_on_failure?
@@ -77,19 +75,8 @@ module Jive
       end
 
       desc "pr URL", "pull request"
-      def pr(url = nil)
-        if url.nil?
-          repo = Rugged::Repository.new(".")
-          remote = repo.remotes.find { |x| x.name == "origin" }
-          return say("origin remote not found") && exit(1) if remote.nil?
-
-          if (match = remote.url.match(SSH_REGEX))
-            url = ["https:/", match[:host], match[:account], match[:repo], repo.head.name].join("/")
-          else
-            uri = URI.parse(remote.url)
-            url = [uri.host, uri.path, repo.head.name].join("/")
-          end
-        end
+      def pr(url = Repo.current.canonical_url)
+        return say("Invalid url") && exit(1) unless url
 
         pr = PullRequest.new(url)
         pr.edit(ENV["EDITOR"])
lib/jive/repo.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Jive
+  class Repo
+    SSH_REGEX = %r{\Agit@(?<host>.+):(?<account>.+)/(?<repo>.+)\z}.freeze
+
+    def initialize(path)
+      @repo = Rugged::Repository.new(path.to_s)
+    end
+
+    def canonical_url
+      remote = @repo.remotes.find { |x| x.name == "origin" }
+      return if remote.nil?
+
+      if (match = remote.url.match(SSH_REGEX))
+        ["https:/", match[:host], match[:account], match[:repo], @repo.head.name].join("/")
+      else
+        uri = URI.parse(remote.url)
+        [uri.host, uri.path, @repo.head.name].join("/")
+      end
+    end
+
+    class << self
+      def current
+        @current ||= new(Pathname.pwd)
+      end
+    end
+  end
+end
lib/jive.rb
@@ -11,6 +11,7 @@ require "jive/git"
 require "jive/popen"
 require "jive/project"
 require "jive/pull_request"
+require "jive/repo"
 require "jive/runner"
 require "jive/shell"
 require "jive/version"
Gemfile.lock
@@ -1,7 +1,7 @@
 PATH
   remote: .
   specs:
-    jive (0.4.3)
+    jive (0.4.4)
       rugged (~> 1.1)
       thor (~> 1.1)