Class: Nucleon::Project::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/nucleon/project/git.rb

Direct Known Subclasses

Github

Instance Method Summary collapse

Instance Method Details

#add_remote_url(name, url, options = {}) ⇒ Object




377
378
379
380
381
382
383
384
385
386
# File 'lib/nucleon/project/git.rb', line 377

def add_remote_url(name, url, options = {})
  return super do |config, processed_url|
    result = cli.remote({
      :add    => true,
      :delete => config.get(:delete, false),
      :push   => config.get(:push, false)
    }, 'set-url', name, processed_url)
    result.status == code.success
  end
end

#add_subproject(path, url, revision, options = {}) ⇒ Object




295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/nucleon/project/git.rb', line 295

def add_subproject(path, url, revision, options = {})
  return super do |config|
    branch_options = ''
    branch_options = [ '-b', config[:revision] ] if config.get(:revision, false)

    path = config[:path]
    url  = config[:url]

    result = cli.submodule({}, 'add', *branch_options, url, path)

    if result.status == code.success
      config.set(:files, [ '.gitmodules', path ])
      true
    else
      false
    end
  end
end

#can_persist?Boolean


Checks

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/nucleon/project/git.rb', line 43

def can_persist?
  ensure_git
  return true unless @repo.nil?
  return false
end

#checkout(revision) ⇒ Object




240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/nucleon/project/git.rb', line 240

def checkout(revision)
  return super do |success|
    if new?
      logger.debug("Project can not be checked out (has not been committed to)")
    else
      unless repo.bare?
        result = cli.checkout({}, revision)
      end
    end
    result && result.status == code.success
  end
end

#cliObject




123
124
125
# File 'lib/nucleon/project/git.rb', line 123

def cli
  @cli
end

#commit(files = '.', options = {}) ⇒ Object




255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/nucleon/project/git.rb', line 255

def commit(files = '.', options = {})
  return super do |config, time, user, message|
    cli.reset({}, 'HEAD') unless new? # Clear the index so we get a clean commit

    files = array(files)

    logger.debug("Adding files to Git index")

    cli.add({}, *files)                  # Get all added and updated files
    cli.add({ :update => true }, *files) # Get all deleted files

    commit_options = {
      :m           => "<#{user}> #{message}",
      :allow_empty => config.get(:allow_empty, false)
    }
    commit_options[:author] = config[:author] if config.get(:author, false)

    logger.debug("Composing commit options: #{commit_options.inspect}")
    result = cli.commit(commit_options)

    if result.status == code.success
      new?(true)
      true
    else
      false
    end
  end
end

#config(name, options = {}) ⇒ Object




138
139
140
141
142
143
144
# File 'lib/nucleon/project/git.rb', line 138

def config(name, options = {})
  return super do |config|
    result = cli.config(config.export, name)
    next Util::Data.value(result.output) if result.status == code.success
    nil
  end
end

#delete_config(name, options = {}) ⇒ Object




157
158
159
160
161
162
# File 'lib/nucleon/project/git.rb', line 157

def delete_config(name, options = {})
  return super do |config|
    result = cli.config(config.import({ :remove_section => true }).export, name)
    result.status == code.success
  end
end

#delete_remote(name) ⇒ Object




390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/nucleon/project/git.rb', line 390

def delete_remote(name)
  return super do
    remote = remote(name)
    if ! remote || remote.empty?
      logger.debug("Project can not delete remote #{name} because it does not exist yet")
      true
    else
      result = cli.remote({}, 'rm', name)
      result.status == code.success
    end
  end
end

#delete_subproject(path) ⇒ Object




316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/nucleon/project/git.rb', line 316

def delete_subproject(path)
  return super do |config|
    path          = config[:path]
    submodule_key = "submodule.#{path}"

    logger.debug("Deleting Git configurations for #{submodule_key}")
    delete_config(submodule_key)
    delete_config(submodule_key, { :file => '.gitmodules' })

    logger.debug("Cleaning Git index cache for #{path}")
    cli.rm({ :cached => true }, path)

    logger.debug("Removing Git submodule directories")
    FileUtils.rm_rf(File.join(directory, path))
    FileUtils.rm_rf(File.join(repo.path, 'modules', path))

    config.set(:files, [ '.gitmodules', path ])
  end
end

#ignore(files) ⇒ Object


Basic Git operations



212
213
214
215
216
# File 'lib/nucleon/project/git.rb', line 212

def ignore(files)
  super do
    ensure_in_gitignore(files)
  end
end

#init_cacheObject


Operations



204
205
206
207
# File 'lib/nucleon/project/git.rb', line 204

def init_cache
  super
  ignore(cache.directory_name)
end

#init_remotesObject


Remote operations



348
349
350
351
352
353
354
355
# File 'lib/nucleon/project/git.rb', line 348

def init_remotes
  return super do
    origin_url = config('remote.origin.url')

    logger.debug("Original origin remote url: #{origin_url}") if origin_url
    origin_url
  end
end

#load_revisionObject




220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/nucleon/project/git.rb', line 220

def load_revision
  return super do
    if new?
      logger.debug("Project has no current revision yet (has not been committed to)")
      nil
    else
      current_revision = nil
      result           = cli.rev_parse({ :abbrev_ref => true }, 'HEAD')

      if result && result.status == code.success
        current_revision = result.output
      end
      logger.debug("Current revision: #{current_revision}")
      current_revision
    end
  end
end

#load_subprojects(options = {}) ⇒ Object


Subproject operations



287
288
289
290
291
# File 'lib/nucleon/project/git.rb', line 287

def load_subprojects(options = {})
  return super do |project_path, data|
    File.exist?(File.join(project_path, '.git'))
  end
end

#new?(reset = false) ⇒ Boolean


Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
# File 'lib/nucleon/project/git.rb', line 101

def new?(reset = false)
  if get(:new, nil).nil? || reset
    result = cli.rev_parse({ :all => true })

    if result && result.status == code.success
      set(:new, result.output.empty?)
    end
  end
  get(:new, false)
end

#normalize(reload) ⇒ Object


Project plugin interface



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nucleon/project/git.rb', line 9

def normalize(reload)
  unless reload
    @cli = Util::Liquid.new do |method, args, &code|
      options = {}
      options = args.shift if args.length > 0
      git_exec(method, options, args, &code)
    end
  end

  @ignore_cache = {}
  super
end

#project_directory?(path, require_top_level = false) ⇒ Boolean


Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nucleon/project/git.rb', line 79

def project_directory?(path, require_top_level = false)
  path    = File.expand_path(path)
  git_dir = File.join(path, '.git')

  if File.exist?(git_dir)
    if File.directory?(git_dir)
      return true
    elsif ! require_top_level
      git_dir = Util::Disk.read(git_dir)
      unless git_dir.nil?
        git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
        return true if File.directory?(git_dir)
      end
    end
  elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
    return true
  end
  return false
end

#pull(remote = :origin, options = {}, &block) ⇒ Object




430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/nucleon/project/git.rb', line 430

def pull(remote = :origin, options = {}, &block)
  return super do |config, processed_remote|
    success = false

    pull_options = {}
    pull_options[:tags] = true if config.get(:tags, true)

    local_revision = config.get(:revision, get(:revision, :master))

    if git_fetch(processed_remote, config)
      if checkout(local_revision)
        result  = cli.pull(pull_options, processed_remote, local_revision, &block)
        success = true if result.status == code.success
      end
    end
    success
  end
end

#push(remote = :edit, options = {}, &block) ⇒ Object




451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/nucleon/project/git.rb', line 451

def push(remote = :edit, options = {}, &block)
  return super do |config, processed_remote|
    push_branch = config.get(:revision, '')

    push_options = {}
    push_options[:all]  = true if push_branch.empty?
    push_options[:tags] = true if ! push_branch.empty? && config.get(:tags, true)

    result = cli.push(push_options, processed_remote, push_branch, &block)
    result.status == code.success
  end
end

#remote(name) ⇒ Object




359
360
361
362
363
364
# File 'lib/nucleon/project/git.rb', line 359

def remote(name)
  return super do
    url = config("remote.#{name}.url")
    url.nil? || url.empty? ? nil : url
  end
end

#set_config(name, value, options = {}) ⇒ Object




148
149
150
151
152
153
# File 'lib/nucleon/project/git.rb', line 148

def set_config(name, value, options = {})
  return super do |config, processed_value|
    result = cli.config(config.export, name, processed_value)
    result.status == code.success
  end
end

#set_location(directory) ⇒ Object




129
130
131
132
133
134
# File 'lib/nucleon/project/git.rb', line 129

def set_location(directory)
  super do
    ensure_git(true)
  end
  return myself
end

#set_remote(name, url) ⇒ Object




368
369
370
371
372
373
# File 'lib/nucleon/project/git.rb', line 368

def set_remote(name, url)
  return super do |processed_url|
    result = cli.remote({}, 'add', name, processed_url)
    result.status == code.success
  end
end

#subproject?(path) ⇒ Boolean


Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nucleon/project/git.rb', line 63

def subproject?(path)
  git_dir = File.join(path, '.git')
  if File.exist?(git_dir)
    unless File.directory?(git_dir)
      git_dir = Util::Disk.read(git_dir)
      unless git_dir.nil?
        git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
        return true if File.directory?(git_dir)
      end
    end
  end
  return false
end

#subproject_config(options = {}) ⇒ Object




166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/nucleon/project/git.rb', line 166

def subproject_config(options = {})
  return super do |config|
    result = {}

    if new?
      logger.debug("Project has no sub project configuration yet (has not been committed to)")
    else
      gitmodules_file = File.join(directory, '.gitmodules')

      gitmodules_data = ''
      gitmodules_data = Util::Disk.read(gitmodules_file) if File.exists?(gitmodules_file)

      unless gitmodules_data.empty?
        logger.debug("Houston, we have some gitmodules!")

        lines   = gitmodules_data.gsub(/\r\n?/, "\n" ).split("\n")
        current = nil

        lines.each do |line|
          if line =~ /^\[submodule "(.+)"\]$/
            current         = $1
            result[current] = {}

            logger.debug("Reading: #{current}")

          elsif line =~ /^\s*(\w+)\s*=\s*(.+)\s*$/
            result[current][$1] = $2
          end
        end
      end
    end
    result
  end
end

#synchronize(cloud, options = {}) ⇒ Object




405
406
407
408
409
410
# File 'lib/nucleon/project/git.rb', line 405

def synchronize(cloud, options = {})
  return super do |config|
    config.init(:remote_path, '/var/git')
    config.set(:add, true)
  end
end

#top?(path) ⇒ Boolean


Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/nucleon/project/git.rb', line 51

def top?(path)
  git_dir = File.join(path, '.git')
  if File.exist?(git_dir)
    return true if File.directory?(git_dir)
  elsif File.exist?(path) && (path =~ /\.git$/ && File.exist?(File.join(path, 'HEAD')))
    return true
  end
  return false
end

#translate_edit_url(url, options = {}) ⇒ Object




478
479
480
481
482
483
484
485
# File 'lib/nucleon/project/git.rb', line 478

def translate_edit_url(url, options = {})
  return super do |config|
    if matches = url.strip.match(/^(https?|git)\:\/\/([^\/]+)\/(.+)/)
      protocol, host, path = matches.captures
      translate_url(host, path, config.import({ :auth => true }))
    end
  end
end

#translate_url(host, path, options = {}) ⇒ Object


Utilities



467
468
469
470
471
472
473
474
# File 'lib/nucleon/project/git.rb', line 467

def translate_url(host, path, options = {})
  return super do |config|
    user = config.get(:user, 'git')
    auth = config.get(:auth, true)

    user + (auth ? '@' : '://') + host + (auth ? ':' : '/') + path
  end
end

#update_subprojects(options = {}) ⇒ Object




338
339
340
341
342
343
# File 'lib/nucleon/project/git.rb', line 338

def update_subprojects(options = {})
  return super do |config|
    result = cli.submodule({}, 'update', '--init', '--recursive')
    result.status == code.success
  end
end