Class: Hiiro::Git::Remote
- Inherits:
-
Object
- Object
- Hiiro::Git::Remote
- Defined in:
- lib/hiiro/git/remote.rb
Instance Attribute Summary collapse
-
#fetch_url ⇒ Object
readonly
Returns the value of attribute fetch_url.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#push_url ⇒ Object
readonly
Returns the value of attribute push_url.
Class Method Summary collapse
Instance Method Summary collapse
- #exists? ⇒ Boolean
- #fetch_remote ⇒ Object
-
#initialize(name:, fetch_url: nil, push_url: nil) ⇒ Remote
constructor
A new instance of Remote.
- #pull(branch = nil) ⇒ Object
- #push(branch = nil, force: false) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(name:, fetch_url: nil, push_url: nil) ⇒ Remote
Returns a new instance of Remote.
26 27 28 29 30 |
# File 'lib/hiiro/git/remote.rb', line 26 def initialize(name:, fetch_url: nil, push_url: nil) @name = name @fetch_url = fetch_url @push_url = push_url end |
Instance Attribute Details
#fetch_url ⇒ Object (readonly)
Returns the value of attribute fetch_url.
4 5 6 |
# File 'lib/hiiro/git/remote.rb', line 4 def fetch_url @fetch_url end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/hiiro/git/remote.rb', line 4 def name @name end |
#push_url ⇒ Object (readonly)
Returns the value of attribute push_url.
4 5 6 |
# File 'lib/hiiro/git/remote.rb', line 4 def push_url @push_url end |
Class Method Details
.all ⇒ Object
6 7 8 9 |
# File 'lib/hiiro/git/remote.rb', line 6 def self.all output = `git remote 2>/dev/null` output.split("\n").map { |name| new(name: name.strip) } end |
.from_verbose_line(line) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hiiro/git/remote.rb', line 15 def self.from_verbose_line(line) # Parses: origin git@github.com:user/repo.git (fetch) match = line.match(/^(\S+)\s+(\S+)\s+\((fetch|push)\)/) return nil unless match name, url, type = match.captures attrs = { name: name } attrs[type == 'fetch' ? :fetch_url : :push_url] = url new(**attrs) end |
.origin ⇒ Object
11 12 13 |
# File 'lib/hiiro/git/remote.rb', line 11 def self.origin new(name: 'origin') end |
Instance Method Details
#exists? ⇒ Boolean
53 54 55 |
# File 'lib/hiiro/git/remote.rb', line 53 def exists? system('git', 'remote', 'get-url', name, out: File::NULL, err: File::NULL) end |
#fetch_remote ⇒ Object
49 50 51 |
# File 'lib/hiiro/git/remote.rb', line 49 def fetch_remote system('git', 'fetch', name) end |
#pull(branch = nil) ⇒ Object
43 44 45 46 47 |
# File 'lib/hiiro/git/remote.rb', line 43 def pull(branch = nil) args = ['git', 'pull', name] args << branch if branch system(*args) end |
#push(branch = nil, force: false) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/hiiro/git/remote.rb', line 36 def push(branch = nil, force: false) args = ['git', 'push', name] args << '-f' if force args << branch if branch system(*args) end |
#to_h ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/hiiro/git/remote.rb', line 61 def to_h { name: name, fetch_url: fetch_url, push_url: push_url, }.compact end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/hiiro/git/remote.rb', line 57 def to_s name end |
#url ⇒ Object
32 33 34 |
# File 'lib/hiiro/git/remote.rb', line 32 def url fetch_url || push_url || fetch_remote_url end |