Class: GitHelpers::GitBranch

Inherits:
Object
  • Object
show all
Defined in:
lib/git_helpers/branch.rb,
lib/git_helpers/raw_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch = "HEAD", dir: ".") ⇒ GitBranch

Returns a new instance of GitBranch.



8
9
10
11
# File 'lib/git_helpers/branch.rb', line 8

def initialize(branch="HEAD", dir: ".")
	@gitdir=dir.is_a?(GitDir) ? dir : GitDir.new(dir)
	@branch=branch
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



5
6
7
# File 'lib/git_helpers/branch.rb', line 5

def branch
  @branch
end

#gitdirObject

Returns the value of attribute gitdir.



4
5
6
# File 'lib/git_helpers/branch.rb', line 4

def gitdir
  @gitdir
end

#infos(*args, name: :default, detached_name: :detached_default) ⇒ Object



52
53
54
55
# File 'lib/git_helpers/branch.rb', line 52

def infos(*args, name: :default, detached_name: :detached_default)
	@infos=infos!(*args) unless @infos
	@infos.merge({name: self.name(method: name, detached_method: detached_name)})
end

Instance Method Details

#==(other) ⇒ Object



196
197
198
# File 'lib/git_helpers/branch.rb', line 196

def ==(other)
	@branch == other.branch && @gitdir=other.gitdir
end

#ahead_behind(br) ⇒ Object



212
213
214
# File 'lib/git_helpers/branch.rb', line 212

def ahead_behind(br)
	@gitdir.ahead_behind(@branch,br)
end

#branch_infosObject



208
209
210
# File 'lib/git_helpers/branch.rb', line 208

def branch_infos
	@gitdir.branch_infos(branch).values.first
end

#checkoutObject



43
44
45
46
47
# File 'lib/git_helpers/branch.rb', line 43

def checkout
	branch=@branch
	branch&.delete_prefix!('refs/heads/') #git checkout refs/heads/master check out in a detached head
	SH.sh! "git checkout #{branch}"
end

#checkout_detachedObject



48
49
50
# File 'lib/git_helpers/branch.rb', line 48

def checkout_detached
	SH.sh! "git checkout #{@branch}~0"
end

#format_infos(**opts) ⇒ Object



81
82
83
# File 'lib/git_helpers/branch.rb', line 81

def format_infos(**opts)
	@gitdir.format_branch_infos([infos], **opts)
end

#full_name(method: :full_name, detached_method: nil, shorten: false, **opts) ⇒ Object



163
164
165
# File 'lib/git_helpers/branch.rb', line 163

def full_name(method: :full_name, detached_method: nil, shorten: false, **opts)
	name(method: method, detached_method: detached_method, shorten: shorten, **opts)
end

#hashObject



192
193
194
# File 'lib/git_helpers/branch.rb', line 192

def hash
	infos["objectname"]
end

#infos!(detached: true) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/git_helpers/branch.rb', line 57

def infos!(detached: true)
	raise GitBranchError.new("Nil Branch #{self}") if nil?
	infos=branch_infos

	if infos.nil?
		if !detached #error out
			raise GitBranchError.new("Detached Branch #{self}")
		else
			infos={detached: true}
			return infos
		end
	end

	type=infos[:type]
	infos[:detached]=false
	if type == :local
		rebase=gitdir.get_config("branch.#{infos["refname:short"]}.rebase")
		rebase = false if rebase.empty?
		rebase = true if rebase == "true"
		infos[:rebase]=rebase
	end
	infos
end

#name(method: :default, detached_method: [:detached_default, :short], shorten: true, highlight_detached: ':', expand_head: true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/git_helpers/branch.rb', line 85

def name(method: :default, detached_method: [:detached_default, :short], shorten: true, highlight_detached: ':', expand_head: true)
	l=lambda { |ev| run_simple(ev, chomp: true, error: :quiet) }
	methods=[*method]
	detached_methods=[*detached_method]
	# we first test each method, then each detached_methods
	method=methods.shift
	if method.nil?
		if !detached_methods.empty?
			describe=self.name(method: detached_methods, detached_method: [], shorten: shorten, highlight_detached: highlight_detached, expand_head: expand_head)
			describe="#{highlight_detached}#{describe}" unless describe.nil? or describe.empty?
			return describe
		else
			return nil
		end
	end
	method="name" if method == :default
	#method="branch-fb" if method == :detached_default
	#method="short" if method == :detached_default
	method="match" if method == :detached_default
	method="branch-fb" if method == :detached_infos
	describe=
		case method.to_s
		when "sha1"
			l.call "git rev-parse #{@branch.shellescape}"
		when "short"
			l.call "git rev-parse --short #{@branch.shellescape}"
		when "symbolic-ref"
			l.call "git symbolic-ref -q --short #{@branch.shellescape}"
		when "describe"
			l.call "git describe #{@branch.shellescape}"
		when "contains"
			l.call "git describe --contains #{@branch.shellescape}"
		when "tags"
			l.call "git describe --tags #{@branch.shellescape}"
		when "match"
			l.call "git describe --all --exact-match #{@branch.shellescape}"
		when "topic"
			l.call "git describe --all #{@branch.shellescape}"
		when "branch"
			l.call "git describe --contains --all #{@branch.shellescape}"
		when "topic-fb" #try --all, then --contains all
			d=l.call "git describe --all #{@branch.shellescape}"
			d=l.call "git describe --contains --all #{@branch.shellescape}" if d.nil? or d.empty?
			d
		when "branch-fb" #try --contains all, then --all
			d=l.call "git describe --contains --all #{@branch.shellescape}"
			d=l.call "git describe --all #{@branch.shellescape}" if d.nil? or d.empty?
			d
		when "magic"
			d1=l.call "git describe --contains --all #{@branch.shellescape}"
			d2=l.call "git describe --all #{@branch.shellescape}"
			d= d1.length < d2.length ? d1 : d2
			d=d1 if d2.empty?
			d=d2 if d1.empty?
			d
		when "name"
			# note: the newer options `git branch --show-current` seems to be
			# the same as this one
			l.call "git rev-parse --abbrev-ref #{@branch.shellescape}"
		when "full_name"
			l.call "git rev-parse --symbolic-full-name #{@branch.shellescape}"
		when "symbolic"
			l.call "git rev-parse --symbolic #{@branch.shellescape}"
		when Proc
			method.call(@branch)
		else
			l.call method unless method.nil? or method.empty?
		end
	if describe.nil? or describe.empty? or describe == "HEAD" && expand_head
		describe=self.name(method: methods, detached_method: detached_method, shorten: shorten, highlight_detached: highlight_detached, expand_head: expand_head)
	end
	if shorten
		describe&.delete_prefix!("refs/")
		describe&.delete_prefix!("heads/")
	end
	return describe
end

#new_branch(name) ⇒ Object



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

def new_branch(name)
	self.class.new(name, dir: @gitdir)
end

#nil?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/git_helpers/branch.rb', line 21

def nil?
	@branch.nil?
end

#push(short: true) ⇒ Object



186
187
188
189
190
191
# File 'lib/git_helpers/branch.rb', line 186

def push(short: true)
	# pu=%x/git rev-parse --abbrev-ref #{@branch.shellescape}@{push}/.chomp!
	br= short ? infos["push:short"] : infos["push"]
	br=nil if br.empty?
	new_branch(br)
end

#push_remoteObject



174
175
176
# File 'lib/git_helpers/branch.rb', line 174

def push_remote
	infos["push:remotename"]
end

#raw_hashObject



101
102
103
# File 'lib/git_helpers/raw_helpers.rb', line 101

def raw_hash
	@hash||=`git rev-parse #{@branch.shellescape}`.chomp!
end

#raw_pushObject



94
95
96
97
98
99
# File 'lib/git_helpers/raw_helpers.rb', line 94

def raw_push
	@gitdir.with_dir do
		pu=%x/git rev-parse --abbrev-ref #{@branch.shellescape}@{push}/.chomp!
		return new_branch(pu)
	end
end

#raw_push_remoteObject



78
79
80
81
82
83
84
85
# File 'lib/git_helpers/raw_helpers.rb', line 78

def raw_push_remote
	@gitdir.with_dir do
		rm= %x/git config --get branch.#{@branch.shellescape}.pushRemote/.chomp! || 
		%x/git config --get remote.pushDefault/.chomp! ||
		remote
		return rm
	end
end

#raw_rebase?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/git_helpers/raw_helpers.rb', line 62

def raw_rebase?
	@gitdir.with_dir do
		rb=%x/git config --bool branch.#{@branch.shellescape}.rebase/.chomp!
		rb||=%x/git config --bool pull.rebase/.chomp!
		return rb=="true"
	end
end

#raw_remoteObject



70
71
72
73
74
75
76
# File 'lib/git_helpers/raw_helpers.rb', line 70

def raw_remote
	@gitdir.with_dir do
		rm=%x/git config --get branch.#{@branch.shellescape}.remote/.chomp!
		rm||="origin"
		return rm
	end
end

#raw_upstreamObject



87
88
89
90
91
92
# File 'lib/git_helpers/raw_helpers.rb', line 87

def raw_upstream
	@gitdir.with_dir do
		up=%x/git rev-parse --abbrev-ref #{@branch.shellescape}@{u}/.chomp!
		return new_branch(up)
	end
end

#rebase?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/git_helpers/branch.rb', line 168

def rebase?
	infos[:rebase]
end

return upstream + push if push !=upstream



201
202
203
204
205
206
# File 'lib/git_helpers/branch.rb', line 201

def related
	up=upstream
	pu=push
	pu=new_branch(nil) if up==pu
	return up, pu
end

#remoteObject



171
172
173
# File 'lib/git_helpers/branch.rb', line 171

def remote
	infos["upstream:remotename"]
end

#reset!Object



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

def reset!
	@infos=nil
end

#run(*args, **kw, &b) ⇒ Object



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

def run(*args,**kw, &b)
	@gitdir.run(*args,**kw, &b)
end

#run_simple(*args, **kw, &b) ⇒ Object



36
37
38
# File 'lib/git_helpers/branch.rb', line 36

def run_simple(*args,**kw,&b)
	@gitdir.run_simple(*args,**kw, &b)
end

#run_success(*args, **kw, &b) ⇒ Object



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

def run_success(*args,**kw,&b)
	@gitdir.run_success(*args,**kw, &b)
end

#shellescapeObject



25
26
27
# File 'lib/git_helpers/branch.rb', line 25

def shellescape
	@branch.shellescape
end

#to_sObject



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

def to_s
	@branch.to_s
end

#upstream(short: true, warn: true) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/git_helpers/branch.rb', line 177

def upstream(short: true, warn: true)
	# up=%x/git rev-parse --abbrev-ref #{@branch.shellescape}@{u}/.chomp!
	br= short ? infos["upstream:short"] : infos["upstream"]
	if br&.empty?
		br=nil
		warn "Warning: Branch #{self} has no upstream" if warn
	end
	new_branch(br)
end