class Nucleon::Project::Git

Public Instance Methods

add_remote_url(name, url, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
377 def add_remote_url(name, url, options = {})
378   return super do |config, processed_url|
379     result = cli.remote({
380       :add    => true,
381       :delete => config.get(:delete, false),
382       :push   => config.get(:push, false)
383     }, 'set-url', name, processed_url)
384     result.status == code.success
385   end
386 end
add_subproject(path, url, revision, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
295 def add_subproject(path, url, revision, options = {})
296   return super do |config|
297     branch_options = ''
298     branch_options = [ '-b', config[:revision] ] if config.get(:revision, false)
299 
300     path = config[:path]
301     url  = config[:url]
302 
303     result = cli.submodule({}, 'add', *branch_options, url, path)
304 
305     if result.status == code.success
306       config.set(:files, [ '.gitmodules', path ])
307       true
308     else
309       false
310     end
311   end
312 end
can_persist?() click to toggle source
   # File lib/nucleon/project/git.rb
43 def can_persist?
44   ensure_git
45   return true unless @repo.nil?
46   return false
47 end
checkout(revision) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
240 def checkout(revision)
241   return super do |success|
242     if new?
243       logger.debug("Project can not be checked out (has not been committed to)")
244     else
245       unless repo.bare?
246         result = cli.checkout({}, revision)
247       end
248     end
249     result && result.status == code.success
250   end
251 end
cli() click to toggle source
    # File lib/nucleon/project/git.rb
123 def cli
124   @cli
125 end
commit(files = '.', options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
255 def commit(files = '.', options = {})
256   return super do |config, time, user, message|
257     cli.reset({}, 'HEAD') unless new? # Clear the index so we get a clean commit
258 
259     files = array(files)
260 
261     logger.debug("Adding files to Git index")
262 
263     cli.add({}, *files)                  # Get all added and updated files
264     cli.add({ :update => true }, *files) # Get all deleted files
265 
266     commit_options = {
267       :m           => "<#{user}> #{message}",
268       :allow_empty => config.get(:allow_empty, false)
269     }
270     commit_options[:author] = config[:author] if config.get(:author, false)
271 
272     logger.debug("Composing commit options: #{commit_options.inspect}")
273     result = cli.commit(commit_options)
274 
275     if result.status == code.success
276       new?(true)
277       true
278     else
279       false
280     end
281   end
282 end
config(name, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
138 def config(name, options = {})
139   return super do |config|
140     result = cli.config(config.export, name)
141     next Util::Data.value(result.output) if result.status == code.success
142     nil
143   end
144 end
delete_config(name, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
157 def delete_config(name, options = {})
158   return super do |config|
159     result = cli.config(config.import({ :remove_section => true }).export, name)
160     result.status == code.success
161   end
162 end
delete_remote(name) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
390 def delete_remote(name)
391   return super do
392     remote = remote(name)
393     if ! remote || remote.empty?
394       logger.debug("Project can not delete remote #{name} because it does not exist yet")
395       true
396     else
397       result = cli.remote({}, 'rm', name)
398       result.status == code.success
399     end
400   end
401 end
delete_subproject(path) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
316 def delete_subproject(path)
317   return super do |config|
318     path          = config[:path]
319     submodule_key = "submodule.#{path}"
320 
321     logger.debug("Deleting Git configurations for #{submodule_key}")
322     delete_config(submodule_key)
323     delete_config(submodule_key, { :file => '.gitmodules' })
324 
325     logger.debug("Cleaning Git index cache for #{path}")
326     cli.rm({ :cached => true }, path)
327 
328     logger.debug("Removing Git submodule directories")
329     FileUtils.rm_rf(File.join(directory, path))
330     FileUtils.rm_rf(File.join(repo.path, 'modules', path))
331 
332     config.set(:files, [ '.gitmodules', path ])
333   end
334 end
ignore(files) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
212 def ignore(files)
213   super do
214     ensure_in_gitignore(files)
215   end
216 end
init_cache() click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
204 def init_cache
205   super
206   ignore(cache.directory_name)
207 end
init_remotes() click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
348 def init_remotes
349   return super do
350     origin_url = config('remote.origin.url')
351 
352     logger.debug("Original origin remote url: #{origin_url}") if origin_url
353     origin_url
354   end
355 end
load_revision() click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
220 def load_revision
221   return super do
222     if new?
223       logger.debug("Project has no current revision yet (has not been committed to)")
224       nil
225     else
226       current_revision = nil
227       result           = cli.rev_parse({ :abbrev_ref => true }, 'HEAD')
228 
229       if result && result.status == code.success
230         current_revision = result.output
231       end
232       logger.debug("Current revision: #{current_revision}")
233       current_revision
234     end
235   end
236 end
load_subprojects(options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
287 def load_subprojects(options = {})
288   return super do |project_path, data|
289     File.exist?(File.join(project_path, '.git'))
290   end
291 end
new?(reset = false) click to toggle source
    # File lib/nucleon/project/git.rb
101 def new?(reset = false)
102   if get(:new, nil).nil? || reset
103     result = cli.rev_parse({ :all => true })
104 
105     if result && result.status == code.success
106       set(:new, result.output.empty?)
107     end
108   end
109   get(:new, false)
110 end
normalize(reload) click to toggle source
Calls superclass method
   # File lib/nucleon/project/git.rb
 9 def normalize(reload)
10   unless reload
11     @cli = Util::Liquid.new do |method, args, &code|
12       options = {}
13       options = args.shift if args.length > 0
14       git_exec(method, options, args, &code)
15     end
16   end
17 
18   @ignore_cache = {}
19   super
20 end
project_directory?(path, require_top_level = false) click to toggle source
   # File lib/nucleon/project/git.rb
79 def project_directory?(path, require_top_level = false)
80   path    = File.expand_path(path)
81   git_dir = File.join(path, '.git')
82 
83   if File.exist?(git_dir)
84     if File.directory?(git_dir)
85       return true
86     elsif ! require_top_level
87       git_dir = Util::Disk.read(git_dir)
88       unless git_dir.nil?
89         git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
90         return true if File.directory?(git_dir)
91       end
92     end
93   elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
94     return true
95   end
96   return false
97 end
pull(remote = :origin, options = {}, &block) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
430 def pull(remote = :origin, options = {}, &block)
431   return super do |config, processed_remote|
432     success = false
433 
434     pull_options = {}
435     pull_options[:tags] = true if config.get(:tags, true)
436 
437     local_revision = config.get(:revision, get(:revision, :master))
438 
439     if git_fetch(processed_remote, config)
440       if checkout(local_revision)
441         result  = cli.pull(pull_options, processed_remote, local_revision, &block)
442         success = true if result.status == code.success
443       end
444     end
445     success
446   end
447 end
push(remote = :edit, options = {}, &block) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
451 def push(remote = :edit, options = {}, &block)
452   return super do |config, processed_remote|
453     push_branch = config.get(:revision, '')
454 
455     push_options = {}
456     push_options[:all]  = true if push_branch.empty?
457     push_options[:tags] = true if ! push_branch.empty? && config.get(:tags, true)
458 
459     result = cli.push(push_options, processed_remote, push_branch, &block)
460     result.status == code.success
461   end
462 end
remote(name) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
359 def remote(name)
360   return super do
361     url = config("remote.#{name}.url")
362     url.nil? || url.empty? ? nil : url
363   end
364 end
set_config(name, value, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
148 def set_config(name, value, options = {})
149   return super do |config, processed_value|
150     result = cli.config(config.export, name, processed_value)
151     result.status == code.success
152   end
153 end
set_location(directory) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
129 def set_location(directory)
130   super do
131     ensure_git(true)
132   end
133   return myself
134 end
set_remote(name, url) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
368 def set_remote(name, url)
369   return super do |processed_url|
370     result = cli.remote({}, 'add', name, processed_url)
371     result.status == code.success
372   end
373 end
subproject?(path) click to toggle source
   # File lib/nucleon/project/git.rb
63 def subproject?(path)
64   git_dir = File.join(path, '.git')
65   if File.exist?(git_dir)
66     unless File.directory?(git_dir)
67       git_dir = Util::Disk.read(git_dir)
68       unless git_dir.nil?
69         git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
70         return true if File.directory?(git_dir)
71       end
72     end
73   end
74   return false
75 end
subproject_config(options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
166 def subproject_config(options = {})
167   return super do |config|
168     result = {}
169 
170     if new?
171       logger.debug("Project has no sub project configuration yet (has not been committed to)")
172     else
173       gitmodules_file = File.join(directory, '.gitmodules')
174 
175       gitmodules_data = ''
176       gitmodules_data = Util::Disk.read(gitmodules_file) if File.exists?(gitmodules_file)
177 
178       unless gitmodules_data.empty?
179         logger.debug("Houston, we have some gitmodules!")
180 
181         lines   = gitmodules_data.gsub(/\r\n?/, "\n" ).split("\n")
182         current = nil
183 
184         lines.each do |line|
185           if line =~ /^\[submodule "(.+)"\]$/
186             current         = $1
187             result[current] = {}
188 
189             logger.debug("Reading: #{current}")
190 
191           elsif line =~ /^\s*(\w+)\s*=\s*(.+)\s*$/
192             result[current][$1] = $2
193           end
194         end
195       end
196     end
197     result
198   end
199 end
synchronize(cloud, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
405 def synchronize(cloud, options = {})
406   return super do |config|
407     config.init(:remote_path, '/var/git')
408     config.set(:add, true)
409   end
410 end
top?(path) click to toggle source
   # File lib/nucleon/project/git.rb
51 def top?(path)
52   git_dir = File.join(path, '.git')
53   if File.exist?(git_dir)
54     return true if File.directory?(git_dir)
55   elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
56     return true
57   end
58   return false
59 end
translate_edit_url(url, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
478 def translate_edit_url(url, options = {})
479   return super do |config|
480     if matches = url.strip.match(/^(https?|git)\:\/\/([^\/]+)\/(.+)/)
481       protocol, host, path = matches.captures
482       translate_url(host, path, config.import({ :auth => true }))
483     end
484   end
485 end
translate_url(host, path, options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
467 def translate_url(host, path, options = {})
468   return super do |config|
469     user = config.get(:user, 'git')
470     auth = config.get(:auth, true)
471 
472     user + (auth ? '@' : '://') + host + (auth ? ':' : '/') + path
473   end
474 end
update_subprojects(options = {}) click to toggle source
Calls superclass method
    # File lib/nucleon/project/git.rb
338 def update_subprojects(options = {})
339   return super do |config|
340     result = cli.submodule({}, 'update', '--init', '--recursive')
341     result.status == code.success
342   end
343 end

Protected Instance Methods

ensure_git(reset = false) click to toggle source
   # File lib/nucleon/project/git.rb
25 def ensure_git(reset = false)
26   if reset || @repo.nil?
27     if directory.empty?
28       logger.warn("Can not manage Git project at #{directory} as it does not exist")
29     else
30       logger.debug("Ensuring Git instance to manage #{directory}")
31       @repo = Util::Git.load(directory, {
32         :create => get(:create, false)
33       })
34     end
35   end
36   return myself
37 end
ensure_in_gitignore(files) click to toggle source
    # File lib/nucleon/project/git.rb
548 def ensure_in_gitignore(files)
549   files        = [ files ] unless files.is_a?(Array)
550   commit_files = nil
551   changes      = false
552 
553   gitignore_file = File.join(directory, '.gitignore')
554   ignore_raw     = Util::Disk.read(gitignore_file)
555   ignores        = []
556   search         = []
557 
558   if ignore_raw && ! ignore_raw.empty?
559     ignores = ignore_raw.split("\n")
560     search  = ignores.collect do |line|
561       line = line.strip
562       ( ! line.empty? && line[0] != '#' ? line : nil )
563     end.compact
564   end
565 
566   files.each do |file|
567     if @ignore_cache[file]
568       found = true
569     else
570       file_regex = Regexp.escape(file)
571       found      = false
572       unless search.empty?
573         search.each do |ignore|
574           if ignore.strip.match(/^#{file_regex}\/?$/)
575             found               = true
576             @ignore_cache[file] = true
577             break
578           end
579         end
580       end
581     end
582     unless found
583       ignores << file
584       changes             = true
585       @ignore_cache[file] = true
586     end
587   end
588   if changes
589     Util::Disk.write(gitignore_file, ignores.join("\n"))
590     commit_files = '.gitignore'
591   end
592   commit_files
593 end
git_exec(command, options = {}, args = []) { |op, cli_command, cli_data| ... } click to toggle source
    # File lib/nucleon/project/git.rb
489 def git_exec(command, options = {}, args = [])
490   result = nil
491 
492   if can_persist?
493     check_value = lambda do |value|
494       next false if value.nil?
495       next false unless value.is_a?(String) || value.is_a?(Symbol)
496       next false if value.to_s.empty?
497       true
498     end
499 
500     localize(repo.workdir) do
501       flags          = []
502       data           = {}
503       processed_args = []
504 
505       options.each do |key, value|
506         cli_option = key.to_s.gsub('_', '-')
507 
508         if value.is_a?(TrueClass) || value.is_a?(FalseClass)
509           flags << cli_option if value == true
510 
511         elsif check_value.call(value)
512           data[cli_option] = value.to_s
513         end
514       end
515 
516       args.each do |value|
517         if check_value.call(value)
518           processed_args << value.to_s
519         end
520       end
521 
522       command_provider = get(:command_provider, Nucleon.type_default(:nucleon, :command))
523       quiet            = get(:quiet, true)
524 
525       command = Nucleon.command({
526         :command => :git,
527         :data    => { 'git-dir=' => repo.path },
528         :subcommand => {
529           :command => command.to_s.gsub('_', '-'),
530           :flags   => flags,
531           :data    => data,
532           :args    => processed_args
533         }
534       }, command_provider)
535 
536       result = command.exec({ :quiet => quiet }) do |op, cli_command, cli_data|
537         block_given? ? yield(op, cli_command, cli_data) : true
538       end
539       Nucleon.remove_plugin(command)
540     end
541   end
542   result
543 end
git_fetch(remote = :origin, options = {}, &block) click to toggle source
    # File lib/nucleon/project/git.rb
415 def git_fetch(remote = :origin, options = {}, &block)
416   config = Config.ensure(options)
417   result = cli.fetch({}, remote, &block)
418 
419   if result.status == code.success
420     new?(true)
421     true
422   else
423     false
424   end
425 end
repo() click to toggle source
    # File lib/nucleon/project/git.rb
115 def repo
116   return @repo if can_persist?
117   return nil
118 end