Class: Remotes

Inherits:
Object
  • Object
show all
Defined in:
lib/remotes.rb,
lib/remotes/remote.rb,
lib/remotes/refspec.rb,
lib/remotes/protocol.rb

Defined Under Namespace

Classes: Protocol, Refspec, Remote

Constant Summary collapse

DEFAULT_REMOTE =
"origin"
InvalidBranch =
Class.new(StandardError)
InvalidRemote =
Class.new(StandardError)
REFSPEC_FORMAT =
/^(\+?)([^:]*)(:([^:]*))?$/

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Remotes

Returns a new instance of Remotes.



11
12
13
# File 'lib/remotes.rb', line 11

def initialize(config)
  @config = config
end

Instance Method Details

#add(name, url, branches = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/remotes.rb', line 15

def add(name, url, branches = [])
  branches = ["*"] if branches.empty?
  @config.open_for_update

  if @config.get(["remote", name, "url"])
    @config.save
    raise InvalidRemote, "remote #{ name } already exists."
  end

  @config.set(["remote", name, "url"], url)

  branches.each do |branch|
    source  = Refs::HEADS_DIR.join(branch)
    target  = Refs::REMOTES_DIR.join(name, branch)
    refspec = Refspec.new(source, target, true)

    @config.add(["remote", name, "fetch"], refspec.to_s)
  end

  @config.save
end

#get(name) ⇒ Object



52
53
54
55
56
57
# File 'lib/remotes.rb', line 52

def get(name)
  @config.open
  return nil unless @config.section?(["remote", name])

  Remote.new(@config, name)
end

#get_upstream(branch) ⇒ Object



59
60
61
62
63
# File 'lib/remotes.rb', line 59

def get_upstream(branch)
  @config.open
  name = @config.get(["branch", branch, "remote"])
  get(name)&.get_upstream(branch)
end

#list_remotesObject



47
48
49
50
# File 'lib/remotes.rb', line 47

def list_remotes
  @config.open
  @config.subsections("remote")
end

#remove(name) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/remotes.rb', line 37

def remove(name)
  @config.open_for_update

  unless @config.remove_section(["remote", name])
    raise InvalidRemote, "No such remote: #{ name }"
  end
ensure
  @config.save
end

#set_upstream(branch, upstream) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
74
# File 'lib/remotes.rb', line 65

def set_upstream(branch, upstream)
  list_remotes.each do |name|
    ref = get(name).set_upstream(branch, upstream)
    return [name, ref] if ref
  end

  raise InvalidBranch,
    "Cannot setup tracking information; " +
    "starting point '#{ upstream }' is not a branch"
end

#unset_upstream(branch) ⇒ Object



76
77
78
79
80
81
# File 'lib/remotes.rb', line 76

def unset_upstream(branch)
  @config.open_for_update
  @config.unset(["branch", branch, "remote"])
  @config.unset(["branch", branch, "merge"])
  @config.save
end