Class: GitCurate::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/git_curate/branch.rb

Constant Summary collapse

CURRENT_BRANCH_REGEX =

Regex for determining whether a “raw” branch name is the name of the current branch on this or another worktree.

/^[+*]\s+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#raw_nameObject (readonly)

Returns the branch name, with “* ” prefixed if it’s the current branch on the current worktree, or “+ ” if it’s the current branch on another worktree.



13
14
15
# File 'lib/git_curate/branch.rb', line 13

def raw_name
  @raw_name
end

#upstream_infoObject (readonly)

Returns a human-friendly string describing the status of the branch relative to the upstream branch it’s tracking, if any.



17
18
19
# File 'lib/git_curate/branch.rb', line 17

def upstream_info
  @upstream_info
end

Class Method Details

.local(merged_opt) ⇒ Object

Returns the local branches



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/git_curate/branch.rb', line 63

def self.local(merged_opt)
  toplevel_dir = Util.command_output("git rev-parse --show-toplevel").strip
  repo = Rugged::Repository.new(toplevel_dir)

  rugged_branches = repo.branches
  repo_head_target = repo.head.target

  command = "git branch" + (merged_opt ? " #{merged_opt}" : "")

  Util.command_to_a(command).map do |line|
    raw_branch_name = line.strip
    proper_branch_name = raw_branch_name.gsub(CURRENT_BRANCH_REGEX, "")
    rugged_branch = rugged_branches[proper_branch_name]
    upstream = rugged_branch.upstream
    upstream_data =
      if upstream
        target_id = rugged_branch.target_id
        ahead, behind = repo.ahead_behind(target_id, upstream.target_id)
        parts = []
        parts << "ahead #{ahead}" if ahead != 0
        parts << "behind #{behind}" if behind != 0
        if parts.any?
          parts.join(", ").capitalize
        else
          "Up to date"
        end
      else
        "No upstream"
      end

    target = rugged_branch.resolve.target
    merged = (repo.merge_base(repo_head_target, target) == target.oid)

    new(
      raw_branch_name,
      merged: merged,
      upstream_info: upstream_data,
    )
  end
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/git_curate/branch.rb', line 24

def current?
  @current ||= (@raw_name =~ CURRENT_BRANCH_REGEX)
end

#displayable_name(pad:) ⇒ Object

Returns the branch’s name, with a prefix of “* ”, if it’s the currently checked out branch, or, if it’s not the currently checked out branch, either a prefix of “ ” (if ‘pad:` receives truthy) or no prefix (if `pad:` received falsey). Branch displayable names are designed to be aligned with each other for display in a vertical column. `pad:` should generally be passed `false` if the caller knows that the current branch won’t be in the list of displayed branches.



38
39
40
41
42
43
44
# File 'lib/git_curate/branch.rb', line 38

def displayable_name(pad:)
  if pad && !current?
    "  #{@raw_name}"
  else
    @raw_name
  end
end

#hashObject



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

def hash
  last_commit.hash
end

#last_authorObject



54
55
56
# File 'lib/git_curate/branch.rb', line 54

def last_author
  last_commit.author
end

#last_commit_dateObject



46
47
48
# File 'lib/git_curate/branch.rb', line 46

def last_commit_date
  last_commit.date
end

#last_subjectObject



58
59
60
# File 'lib/git_curate/branch.rb', line 58

def last_subject
  last_commit.subject
end

#merged?Boolean

Return truthy if and only if this branch has been merged into the current HEAD.

Returns:

  • (Boolean)


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

def merged?
  @merged
end

#proper_nameObject

Returns simply the name of the branch, without any other “decoration”.



20
21
22
# File 'lib/git_curate/branch.rb', line 20

def proper_name
  @proper_name ||= @raw_name.lstrip.sub(CURRENT_BRANCH_REGEX, '')
end