Class: SRC::Git::Branch

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

Constant Summary collapse

VERSION_FILE =
'version'
AHEAD =
/# Your branch is ahead/
BEHIND =
/# Your branch is behind/
DIVERGED =
/# Your branch and .* have diverged/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Branch

Returns a new instance of Branch.



22
23
24
# File 'lib/src/git/branch.rb', line 22

def initialize(name)
  @name = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/src/git/branch.rb', line 9

def name
  @name
end

Class Method Details

.checked_outObject



12
13
14
# File 'lib/src/git/branch.rb', line 12

def checked_out
  new(`git rev-parse --abbrev-ref HEAD`.chomp)
end

.latest(prefix) ⇒ Object



16
17
18
19
# File 'lib/src/git/branch.rb', line 16

def latest(prefix)
  branch_name = `git branch`.split(/\s+/).select { |b| b =~ /\A#{prefix}/ }.max
  new(branch_name) if branch_name
end

Instance Method Details

#==(other) ⇒ Object



124
125
126
# File 'lib/src/git/branch.rb', line 124

def ==(other)
  name == other.to_s
end

#add(filename) ⇒ Object



65
66
67
68
69
# File 'lib/src/git/branch.rb', line 65

def add(filename)
  checked_out do
    raise unless system("git add #{filename}")
  end
end

#branch_from(new_branch) ⇒ Object



71
72
73
74
75
76
# File 'lib/src/git/branch.rb', line 71

def branch_from(new_branch)
  checked_out do
    raise unless system("git branch #{new_branch}")
  end
  self.class.new(new_branch)
end

#checked_outObject



78
79
80
81
82
83
84
# File 'lib/src/git/branch.rb', line 78

def checked_out
  previous_branch = self.class.checked_out
  self.checkout
  yield
ensure
  previous_branch.checkout
end

#checked_out?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/src/git/branch.rb', line 35

def checked_out?
  self.class.checked_out == self
end

#checkoutObject



30
31
32
33
# File 'lib/src/git/branch.rb', line 30

def checkout
  msg = `git checkout #{name} -q`
  raise msg unless $?.success?
end

#commit(msg) ⇒ Object



59
60
61
62
63
# File 'lib/src/git/branch.rb', line 59

def commit(msg)
  checked_out do
    raise unless system("git commit -m '#{msg}' -q")
  end
end

#exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/src/git/branch.rb', line 26

def exists?
  system("git show-ref --verify --quiet refs/heads/'#{name}'")
end

#merge(other_branch) ⇒ Object



86
87
88
89
90
# File 'lib/src/git/branch.rb', line 86

def merge(other_branch)
  checked_out do
    raise unless system("git merge --no-ff #{other_branch}")
  end
end

#remote_ahead?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/src/git/branch.rb', line 104

def remote_ahead?
  status =~ AHEAD
end

#remote_behind?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/src/git/branch.rb', line 108

def remote_behind?
  status =~ BEHIND
end

#remote_diverged?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/src/git/branch.rb', line 112

def remote_diverged?
  status =~ DIVERGED
end

#remote_up_to_date?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/src/git/branch.rb', line 116

def remote_up_to_date?
  !(remote_diverged? || remote_behind? || remote_ahead?)
end

#statusObject



98
99
100
101
102
# File 'lib/src/git/branch.rb', line 98

def status
  checked_out do
    `git status -uno`.chomp
  end
end

#subset_of?(other_branch) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/src/git/branch.rb', line 39

def subset_of?(other_branch)
  `git rev-list #{other_branch}..#{name}`.chomp.length == 0
end

#tagObject



92
93
94
95
96
# File 'lib/src/git/branch.rb', line 92

def tag
  checked_out do
    raise unless system("git tag #{version}")
  end
end

#to_sObject



120
121
122
# File 'lib/src/git/branch.rb', line 120

def to_s
  name
end

#update_version_file(new_version) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/src/git/branch.rb', line 51

def update_version_file(new_version)
  checked_out do
    raise unless system("echo '#{new_version}' > #{version_file}")
    self.add(version_file)
    self.commit("version bumped to #{new_version}")
  end
end

#versionObject



43
44
45
# File 'lib/src/git/branch.rb', line 43

def version
  `git show #{name}:#{version_file}`.chomp
end

#version_fileObject



47
48
49
# File 'lib/src/git/branch.rb', line 47

def version_file
  VERSION_FILE
end