Class: Branchtree::Branch

Inherits:
Object
  • Object
show all
Includes:
Context
Defined in:
lib/branchtree/branch.rb

Overview

Represents a git branch in the current repository.

Defined Under Namespace

Classes: Info, InvalidInfo, NullInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context

logger

Constructor Details

#initialize(name, parent, rebase) ⇒ Branch

Returns a new instance of Branch.



21
22
23
24
25
26
27
# File 'lib/branchtree/branch.rb', line 21

def initialize(name, parent, rebase)
  @name = name
  @parent = parent
  @rebase = rebase
  @children = []
  @info = NullInfo.new(self)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



18
19
20
# File 'lib/branchtree/branch.rb', line 18

def children
  @children
end

#infoObject

Returns the value of attribute info.



19
20
21
# File 'lib/branchtree/branch.rb', line 19

def info
  @info
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/branchtree/branch.rb', line 18

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/branchtree/branch.rb', line 18

def parent
  @parent
end

Class Method Details

.load(node, parent) ⇒ Object

Recursively load a Branch instance and its children, if any, from deserialized YAML.



9
10
11
12
13
14
15
16
# File 'lib/branchtree/branch.rb', line 9

def self.load(node, parent)
  new(node.fetch("branch"), parent, node.fetch("rebase", false)).tap do |branch|
    node.fetch("children", []).each do |child_node|
      branch.children << load(child_node, branch)
    end
    branch.children.freeze
  end
end

Instance Method Details

#checkoutObject

Checkout this branch with git



55
56
57
# File 'lib/branchtree/branch.rb', line 55

def checkout
  qcmd.run("git", "checkout", name)
end

#full_refObject

Return the full git ref name of this branch.



50
51
52
# File 'lib/branchtree/branch.rb', line 50

def full_ref
  "refs/heads/#{name}"
end

#merge_parentObject



59
60
61
# File 'lib/branchtree/branch.rb', line 59

def merge_parent
  qcmd.run!("git", "merge", parent_branch_name)
end

#parent_branch_nameObject

Return the String name of the ref that this branch is based on. New changes to this parent ref will be merged in on “apply”.



39
40
41
42
43
44
45
46
47
# File 'lib/branchtree/branch.rb', line 39

def parent_branch_name
  return @parent.name if @parent

  if cmd.run!("git", "rev-parse", "--verify", "--quiet", "refs/heads/main").success?
    "main"
  else
    "master"
  end
end

#rebase?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/branchtree/branch.rb', line 33

def rebase?
  @rebase
end

#rebase_parentObject



63
64
65
# File 'lib/branchtree/branch.rb', line 63

def rebase_parent
  qcmd.run!("git", "rebase", parent_branch_name)
end

#root?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/branchtree/branch.rb', line 29

def root?
  @parent.nil?
end