Class: Hiiro::Git::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/git/remote.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_urlObject (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

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hiiro/git/remote.rb', line 4

def name
  @name
end

#push_urlObject (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

.allObject



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

.originObject



11
12
13
# File 'lib/hiiro/git/remote.rb', line 11

def self.origin
  new(name: 'origin')
end

Instance Method Details

#exists?Boolean

Returns:

  • (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_remoteObject



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_hObject



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_sObject



57
58
59
# File 'lib/hiiro/git/remote.rb', line 57

def to_s
  name
end

#urlObject



32
33
34
# File 'lib/hiiro/git/remote.rb', line 32

def url
  fetch_url || push_url || fetch_remote_url
end