Class: Origen::RevisionControl::Git
- Inherits:
-
Base
show all
- Defined in:
- lib/origen/revision_control/git.rb
Instance Attribute Summary
Attributes inherited from Base
#local, #remote, #remotes_method
Class Method Summary
collapse
-
.origin ⇒ Object
Returns the origin for the PWD.
-
.user_email ⇒ Object
A class method is provided to fetch the user email since it is useful to have access to this when outside of an application workspace, e.g.
-
.user_name ⇒ Object
A class method is provided to fetch the user name since it is useful to have access to this when outside of an application workspace, e.g.
-
.version ⇒ Object
Returns the Git version number from the current runtime environment (as a string).
Instance Method Summary
collapse
Methods inherited from Base
#dssc?, #git?, #initialize, #p4?, #svn?
Class Method Details
Returns the origin for the PWD
5
6
7
8
9
10
11
12
|
# File 'lib/origen/revision_control/git.rb', line 5
def self.origin
git('remote --verbose', verbose: false).each do |remote|
if remote =~ /^origin\s+([^\s]+)/
return Regexp.last_match(1)
end
end
nil
end
|
.user_email ⇒ Object
A class method is provided to fetch the user email since it is useful to have access to this when outside of an application workspace, e.g. when creating a new app
305
306
307
308
309
|
# File 'lib/origen/revision_control/git.rb', line 305
def self.user_email
git('config user.email', verbose: false).first
rescue
nil
end
|
.user_name ⇒ Object
A class method is provided to fetch the user name since it is useful to have access to this when outside of an application workspace, e.g. when creating a new app
297
298
299
300
301
|
# File 'lib/origen/revision_control/git.rb', line 297
def self.user_name
git('config user.name', verbose: false).first
rescue
nil
end
|
Returns the Git version number from the current runtime environment (as a string)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/origen/revision_control/git.rb', line 15
def self.version
@version ||= begin
version = nil
git('--version', verbose: false).each do |line|
if line =~ /git version (\d+(\.\d+)+)/
version = Regexp.last_match(1)
break
end
end
if version
version
else
Origen.log.warning 'Failed to determine the current Git version, proceeding by assuming version 2.0.0'
'2.0.0'
end
end
end
|
Instance Method Details
#build(options = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/origen/revision_control/git.rb', line 33
def build(options = {})
if Dir["#{local}/*"].empty? || options[:force]
FileUtils.rm_rf(local.to_s)
system "git clone #{remote} #{local}"
else
fail "The requested workspace is not empty: #{local}"
end
end
|
#can_checkin? ⇒ Boolean
Returns true if the current user can checkin to the given repo (means has permission to push in Git terms)
150
151
152
153
154
155
156
|
# File 'lib/origen/revision_control/git.rb', line 150
def can_checkin?
git('push --dry-run origin origin:refs/heads/OrigenWritePermissionsTest', verbose: false)
true
rescue
false
end
|
#changes(dir = nil, options = {}) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/origen/revision_control/git.rb', line 158
def changes(dir = nil, options = {})
paths, options = clean_path(dir, options)
options = {
verbose: false
}.merge(options)
git 'fetch', options
version = options[:version] || 'HEAD'
objects = {}
objects[:added] = git("diff --name-only --diff-filter=A #{version} #{paths.first}", options).map(&:strip)
objects[:removed] = git("diff --name-only --diff-filter=D #{version} #{paths.first}", options).map(&:strip)
objects[:changed] = git("diff --name-only --diff-filter=M #{version} #{paths.first}", options).map(&:strip)
objects[:present] = !objects[:added].empty? || !objects[:removed].empty? || !objects[:changed].empty?
objects[:added].map! { |i| "#{paths.first}/" + i }
objects[:removed].map! { |i| "#{paths.first}/" + i }
objects[:changed].map! { |i| "#{paths.first}/" + i }
objects
end
|
#checkin(path = nil, options = {}) ⇒ Object
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
|
# File 'lib/origen/revision_control/git.rb', line 95
def checkin(path = nil, options = {})
paths, options = clean_path(path, options)
if options[:force] && !options[:initial]
checkin(paths.join(' '), no_push: true, verbose: false, comment: options[:comment])
local_rev = current_commit(short: false)
checkout
git("checkout #{local_rev} -- #{paths.join(' ')}", check_errors: false)
else
checkout unless options[:initial]
end
cmd = 'add'
if options[:unmanaged]
cmd += ' -A'
else
cmd += ' -u' unless options[:unmanaged]
end
cmd += " #{paths.join(' ')}"
git cmd, options
if changes_pending_commit?
cmd = 'commit'
if options[:comment] && !options[:comment].strip.empty?
cmd += " -m \"#{options[:comment].strip}\""
else
cmd += " -m \"No comment!\""
end
if options[:author]
if options[:author].respond_to?(:name_and_email)
author = options[:author].name_and_email
else
author = "#{options[:author]} <>"
end
cmd += " --author=\"#{author}\""
end
if options[:time]
cmd += " --date=\"#{options[:time].strftime('%a %b %e %H:%M:%S %Y %z')}\""
end
git cmd, options
end
unless options[:no_push]
cmd = "push origin #{current_branch}"
cmd += ' -u' if options[:initial]
git cmd
end
paths
end
|
#checkout(path = nil, options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
|
# File 'lib/origen/revision_control/git.rb', line 43
def checkout(path = nil, options = {})
paths, options = clean_path(path, options)
git 'fetch', options
version = options[:version] || current_branch
if version == 'HEAD'
puts "Sorry, but you are not currently on a branch and I don't know which branch you want to checkout"
puts 'Please supply a branch name as the version to checkout the latest version of it, e.g. origen rc co -v develop'
exit 1
end
if options[:force]
version = "origin/#{version}" if remote_branch?(version)
if paths == [local.to_s]
git "reset --hard #{version}", options
else
git 'reset HEAD'
git 'pull', options
git "checkout #{version} #{paths.join(' ')}", options
end
else
if paths.size > 1 || paths.first != local.to_s
fail 'The Git driver does not support partial merge checkout, it has to be the whole workspace'
end
git 'reset HEAD'
res = git 'stash', options
stashed = !res.any? { |l| l =~ /^No local changes to save/ }
git 'pull', options
git "checkout #{version}", options
if stashed
result = git 'stash pop', { check_errors: false }.merge(options)
conflicts = []
result.each do |line|
if line =~ /CONFLICT.* (.*)$/
conflicts << Regexp.last_match(1)
end
end
git 'reset HEAD'
unless conflicts.empty?
Origen.log.info ''
Origen.log.error 'Your local changes could not automatically merged into the following files, open them to fix the conflicts:'
conflicts.each do |conflict|
Origen.log.error " #{conflict}"
end
end
end
end
paths
end
|
#current_branch ⇒ Object
226
227
228
|
# File 'lib/origen/revision_control/git.rb', line 226
def current_branch
git('rev-parse --abbrev-ref HEAD', verbose: false).first
end
|
#current_commit(options = {}) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/origen/revision_control/git.rb', line 230
def current_commit(options = {})
options = {
short: true
}.merge(options)
commit = git('rev-parse HEAD', verbose: false).first
if options[:short]
commit[0, 11]
else
commit
end
end
|
#delete_all(dir = nil, options = {}) ⇒ Object
Delete everything in the given directory, or the whole repo
289
290
291
292
293
|
# File 'lib/origen/revision_control/git.rb', line 289
def delete_all(dir = nil, options = {})
paths, options = clean_path(dir, options)
files = git("ls-files #{paths.first}")
FileUtils.rm_f files
end
|
#diff_cmd(file, version = nil) ⇒ Object
203
204
205
206
207
208
209
|
# File 'lib/origen/revision_control/git.rb', line 203
def diff_cmd(file, version = nil)
if version
"git difftool --tool tkdiff -y #{prefix_tag(version)} #{file}"
else
"git difftool --tool tkdiff -y #{file}"
end
end
|
#github? ⇒ Boolean
Returns true if the remote points to a github url
320
321
322
|
# File 'lib/origen/revision_control/git.rb', line 320
def github?
!!(remote.to_s =~ /github.com/)
end
|
#initialized?(options = {}) ⇒ Boolean
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/origen/revision_control/git.rb', line 267
def initialized?(options = {})
@hierarchy_searched ||= begin
path = @local.dup
until path.root? || File.exist?("#{local}/.git")
if File.exist?("#{path}/.git")
if options[:allow_local_adjustment]
@local = path
else
fail "Requested local repository #{local} is within existing local repository #{path}"
end
else
path = path.parent
end
end
true
end
File.exist?("#{local}/.git") &&
git('remote -v', verbose: false).any? { |r| r =~ /#{remote_without_protocol_and_user}/ || r =~ /#{remote_without_protocol_and_user.to_s.gsub(':', "\/")}/ } &&
!git('status', verbose: false).any? { |l| l =~ /^#? ?(Initial commit|No commits yet)$/ }
end
|
#local_modifications(dir = nil, options = {}) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/origen/revision_control/git.rb', line 181
def local_modifications(dir = nil, options = {})
paths, options = clean_path(dir, options)
options = {
verbose: false
}.merge(options)
cmd = 'diff --name-only'
dir = " #{paths.first}"
unstaged = git(cmd + dir, options).map(&:strip)
staged = git(cmd + ' --cached' + dir, options).map(&:strip)
unstaged + staged
end
|
#remote_branch?(str) ⇒ Boolean
Returns true if the given string matches a branch name in the remote repo
Origen.app.rc.remote_branch?("master") Origen.app.rc.remote_branch?("feature/exists") Origen.app.rc.remote_branch?("feature/does_not_exist")
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/origen/revision_control/git.rb', line 253
def remote_branch?(str)
if github?
rem = remote_without_protocol
else
rem = remote
end
git("ls-remote --heads #{remote} #{str}", verbose: false).any? do |line|
line =~ /^[0-9a-f]{40}\s+[a-zA-Z]/
end
end
|
222
223
224
|
# File 'lib/origen/revision_control/git.rb', line 222
def root
Pathname.new(git('rev-parse --show-toplevel', verbose: false).first.strip)
end
|
#tag(id, options = {}) ⇒ Object
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/origen/revision_control/git.rb', line 211
def tag(id, options = {})
id = VersionString.new(id)
id = id.prefixed if id.semantic?
if options[:comment]
git "tag -a #{id} -m \"#{options[:comment]}\""
else
git "tag #{id}"
end
git "push origin #{id}"
end
|
#tag_exists?(tag) ⇒ Boolean
Returns true if the given tag already exists
243
244
245
246
247
|
# File 'lib/origen/revision_control/git.rb', line 243
def tag_exists?(tag)
git('fetch', verbose: false) unless @all_tags_fetched
@all_tags_fetched = true
git('tag', verbose: false).include?(tag.to_s)
end
|
#unmanaged(dir = nil, options = {}) ⇒ Object
194
195
196
197
198
199
200
201
|
# File 'lib/origen/revision_control/git.rb', line 194
def unmanaged(dir = nil, options = {})
paths, options = clean_path(dir, options)
options = {
verbose: false
}.merge(options)
cmd = "ls-files #{paths.first} --exclude-standard --others"
git(cmd, options).map(&:strip)
end
|
#user_email ⇒ Object
315
316
317
|
# File 'lib/origen/revision_control/git.rb', line 315
def user_email
self.class.user_email
end
|
#user_name ⇒ Object
311
312
313
|
# File 'lib/origen/revision_control/git.rb', line 311
def user_name
self.class.user_name
end
|