Class: Redcar::Scm::Git::Manager

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
plugins/scm_git/lib/scm_git.rb

Constant Summary

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Model

#adapter, #merge!, #pull!, #pull_targets, #remote_init, #translations

Instance Attribute Details

- (Object) path (readonly)

SCM hooks



148
149
150
# File 'plugins/scm_git/lib/scm_git.rb', line 148

def path
  @path
end

- (Object) repo (readonly)

SCM hooks



148
149
150
# File 'plugins/scm_git/lib/scm_git.rb', line 148

def repo
  @repo
end

Class Method Details

+ (Object) debug

Whether to print debugging messages. Default to whatever scm is using.



38
39
40
# File 'plugins/scm_git/lib/scm_git.rb', line 38

def self.debug
  Redcar::Scm::Manager.debug
end

+ (Object) scm_module

SCM plugin hooks



22
23
24
# File 'plugins/scm_git/lib/scm_git.rb', line 22

def self.scm_module
  Redcar::Scm::Git::Manager
end

+ (Boolean) supported?

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'plugins/scm_git/lib/scm_git.rb', line 26

def self.supported?
  begin
    if ::Git::Lib.new.meets_required_version?
      return true
    end
  rescue
    puts $!.class.name + ": " + $!.message
  end
  false
end

Instance Method Details

- (Array<String>) branches

REQUIRED for :switch_branch. Returns an array of branch names.

Returns:



439
440
441
442
# File 'plugins/scm_git/lib/scm_git.rb', line 439

def branches
  branch_list = cache['branches'].map {|b| b[0]}
  branch_list + ['New...']
end

- (Object) cache



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
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
# File 'plugins/scm_git/lib/scm_git.rb', line 68

def cache
  @cache ||= begin
    c = ::BlockCache.new

    c.add('branches', 15) do
      ref_path = File.join(@repo.dir.path, '.git', 'refs', 'heads');
      head = File.read(File.join(@repo.dir.path, '.git', 'HEAD')).strip
      branch_globs = Dir.glob(File.join(ref_path, '*'))
      branches = []

      while branch = branch_globs.shift
        if File.directory?(branch)
          branch_globs.push(*Dir.glob(File.join(branch, '*')))
        else
          branches.push branch
        end
      end

      branches.map {|b|
        b[ref_path.length + 1, b.length - ref_path.length - 1]
      }.map {|b|
        [b, ('ref: refs/heads/' + b == head)]
      }
    end

    c.add('all branches', 15) do
      raise "not implemented"
    end

    c.add('status', 5) { @repo.status }

    c.add('full status', 5) { @repo.lib.full_status }

    c.add('config', 30) do
      config = Scm::Git::ConfigFile.parse(File.join(@repo.dir.path, '.git', 'config'))
      conf = {}

      config.each do |key, values|
        prefix = key.sub(/^([a-z]+) "(.+)"$/i, '\1.\2')

        values.each do |key2, value|
          conf[prefix + '.' + key2] = value
        end
      end

      conf
    end

    c.add('log', 60*60) do |start, finish|
      @repo.lib.log_commits(:between => [start, finish]).reverse.map do |c|
        @repo.gcommit(c)
      end
    end

    c.add('submodules', 60*60) do
      begin
        modules = Scm::Git::ConfigFile.parse(File.join(@repo.dir.path, '.gitmodules'))

        mods = {}
        modules.each {|k, v|
          path = File.join(@repo.dir.path, v['path'])
          if File.exist?(File.join(path, '.git'))
            mod = Scm::Git::Manager.new
            mod.load(path)
            mods[v['path']] = mod
          end
        }
        mods
      rescue Errno::ENOENT => e
        {}
      end
    end

    c
  end
end

- (Object) commit!(message, change = nil)

REQUIRED for :commit. Commits the currently indexed changes in the subproject.

you don't provide these.

Parameters:

  • change (defaults to: nil)

    Required for :commitable changes. Ignore if



402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'plugins/scm_git/lib/scm_git.rb', line 402

def commit!(message, change=nil)
  if change
    # delegate to the proper submodule
    if self != change.repo
      return change.repo.commit!(change)
    end

    # redelegate the commit to the subproject to handle
    cache['submodules'][change.path].commit!(message)
  else
    @repo.commit(message)
    cache.refresh
  end
end

- (Object) commit_message(change = nil)

REQUIRED for :commit. Gets a default commit message for the currently indexed changes.

you don't provide these.

Parameters:

  • change (defaults to: nil)

    Required for :commitable changes. Ignore if



422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'plugins/scm_git/lib/scm_git.rb', line 422

def commit_message(change=nil)
  if change
    # delegate to the proper submodule
    if self != change.repo
      return change.repo.commit!(change)
    end

    # redelegate the call to the subproject to handle
    cache['submodules'][change.path].commit_message
  else
    "\n\n" + cache['full status']
  end
end

- (Object) current_branch

REQUIRED for :switch_branch. Returns the name of the current branch.



445
446
447
448
449
# File 'plugins/scm_git/lib/scm_git.rb', line 445

def current_branch
  b = cache['branches'].select { |b| b[1] }.first

  b.nil? ? "" : b[0]
end

- (Object) debug



42
43
44
# File 'plugins/scm_git/lib/scm_git.rb', line 42

def debug
  Redcar::Scm::Git::Manager.debug
end

- (Object) from_data(data)



46
47
48
49
50
51
52
53
54
# File 'plugins/scm_git/lib/scm_git.rb', line 46

def from_data(data)
  data = data.split(':')
  file = ::Git::Status::StatusFile.new(nil, {
    :type_raw => data[0],
    :path => data[1],
  })
  repo = Scm::Git::Manager.new.load(data[2])
  Scm::Git::Change.new(file, repo, :file, data[4] == "true")
end

- (Object) index_add(change)

REQUIRED for :index. Adds a new file to the index.



258
259
260
261
262
263
264
265
266
267
# File 'plugins/scm_git/lib/scm_git.rb', line 258

def index_add(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_add(change)
  end

  @repo.add(change.path)
  cache.refresh
  true # refresh trees
end

- (Object) index_delete(change)

REQUIRED for :index. Marks a file as deleted in the index.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'plugins/scm_git/lib/scm_git.rb', line 382

def index_delete(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_delete(change)
  end

  if change.git_status[1,1] == '?'
    FileUtils.rm(File.join(@repo.dir.path, change.path))
  else
    @repo.remove(change.path)
  end
  cache.refresh
  true # refresh trees
end

- (Object) index_ignore(change)

REQUIRED for :index. Ignores a new file so it won't show in changes.



270
271
272
273
274
275
276
277
278
279
280
# File 'plugins/scm_git/lib/scm_git.rb', line 270

def index_ignore(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_ignore(change)
  end

  add_to_gitignore(change.path)

  cache.refresh
  true # refresh trees
end

- (Object) index_ignore_all(extension, change)

REQUIRED for :index. Ignores all files with a certain extension so they won't show in changes.



284
285
286
287
288
289
290
291
292
293
294
# File 'plugins/scm_git/lib/scm_git.rb', line 284

def index_ignore_all(extension, change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_ignore(change)
  end

  add_to_gitignore("*." + extension)

  cache.refresh
  true # refresh trees
end

- (Object) index_restore(change)

REQUIRED for :index. Restores a file to the last known state of the file. This may be from the index, or the last commit.



370
371
372
373
374
375
376
377
378
379
# File 'plugins/scm_git/lib/scm_git.rb', line 370

def index_restore(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_restore(change)
  end

  @repo.checkout_file('HEAD', change.path)
  cache.refresh
  true # refresh trees
end

- (Object) index_revert(change)

REQUIRED for :index. Reverts a file to its last commited state.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'plugins/scm_git/lib/scm_git.rb', line 321

def index_revert(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_revert(change)
  end

  if change.git_status[0,1] != ' '
    # Git requires us to unindex any changes before we can revert them.
    index_unsave(change)
  end
  if change.git_status[0,1] == 'A' or change.git_status[0,1] == '?'
    # if this was a new file, then revert it by deleting it
    File.unlink(File.join(@repo.dir.path, change.path))
  else
    # otherwise checkout the old version
    @repo.checkout_file('HEAD', change.path)
  end
  cache.refresh
  true # refresh trees
end

- (Object) index_save(change)

REQUIRED for :index. Saves changes made to a file in the index.



357
358
359
360
361
362
363
364
365
366
# File 'plugins/scm_git/lib/scm_git.rb', line 357

def index_save(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_save(change)
  end

  @repo.add(change.path)
  cache.refresh
  true # refresh trees
end

- (Object) index_unsave(change)

REQUIRED for :index. Reverts a file in the index back to it's last commited state, but leaves the file intact.



344
345
346
347
348
349
350
351
352
353
354
# File 'plugins/scm_git/lib/scm_git.rb', line 344

def index_unsave(change)
  # delegate to the proper submodule
  if self != change.repo
    return change.repo.index_unsave(change)
  end

  @repo.reset('HEAD', :file => change.path)

  cache.refresh
  true # refresh trees
end

- (Array<Redcar::Scm::ScmChangeMirror::Change>) indexed_changes

Returns:

  • (Array<Redcar::Scm::ScmChangeMirror::Change>)


197
198
199
# File 'plugins/scm_git/lib/scm_git.rb', line 197

def indexed_changes
  prepare_changes(true)
end

- (Object) init!(path)



176
177
178
179
180
181
# File 'plugins/scm_git/lib/scm_git.rb', line 176

def init!(path)
  # Be nice and don't blow away another repository accidentally.
  return nil if File.exist?(File.join(path, %w{.git}))

  ::Git.init(path)
end

- (Object) inspect

General stuff



60
61
62
63
64
65
66
# File 'plugins/scm_git/lib/scm_git.rb', line 60

def inspect
  if @repo
    %Q{#<Scm::Git::Manager "#{@repo.dir.path}">}
  else
    %Q{#<Scm::Git::Manager>}
  end
end

- (Object) load(path)



183
184
185
186
187
188
189
# File 'plugins/scm_git/lib/scm_git.rb', line 183

def load(path)
  raise "Already loaded repository" if @repo
  @repo = ::Git.open(repository_path(path))
  @path = path
  cache.refresh
  self
end

- (Object) push!(branch = current_branch)

REQUIRED for :push. Pushes all current changesets to the remote repository.



527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'plugins/scm_git/lib/scm_git.rb', line 527

def push!(branch=current_branch)
  remote = cache['config']['branch.' + branch + '.remote']
  push_target = cache['config']['branch.' + branch + '.push'] || cache['config']['branch.' + branch + '.merge']

  # don't block while trying to push changes
  Thread.new do
    repo.push(remote, '+refs/heads/' + branch + ':' + push_target)

    Redcar.update_gui { cache.refresh; Scm::Manager.refresh_trees }
  end

  false # don't refresh trees
end

- (Object) push_targets



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'plugins/scm_git/lib/scm_git.rb', line 471

def push_targets
  targets = branches.map {|b| Scm::ScmCommitsMirror::CommitsNode.new(self, b)}
  modules = cache['submodules'].clone

  while m = modules.shift
    path = m[0]
    m[1].cache['submodules'].each {|k,v| modules[File.join(path, k)] = v}

    targets += m[1].branches.map {|b| Scm::ScmCommitsMirror::CommitsNode.new(m[1], b, "#{b} (#{path})")}
  end

  # only return targets we can actually push to
  targets.find_all do |t|
    remote = t.repo.cache['config']['branch.' + t.branch + '.remote']

    if remote
      push_target = t.repo.cache['config']['branch.' + t.branch + '.push'] || t.repo.cache['config']['branch.' + t.branch + '.merge']

      push_target.gsub!(/^refs\/heads\//, '')
      r_ref_file = File.join(t.repo.repo.dir.path, '.git', 'refs', 'remotes', remote, push_target)

      File.exist?(r_ref_file)
    end
  end
end

- (Object) refresh



172
173
174
# File 'plugins/scm_git/lib/scm_git.rb', line 172

def refresh
  cache.refresh
end

- (Boolean) repository?(path)

Returns:

  • (Boolean)


154
155
156
# File 'plugins/scm_git/lib/scm_git.rb', line 154

def repository?(path)
  return true if repository_path(path)
end

- (Object) repository_path(path)



158
159
160
161
162
163
164
165
166
# File 'plugins/scm_git/lib/scm_git.rb', line 158

def repository_path(path)
  if File.exist?(File.join(path, %w{.git}))
    return path
  else
    unless path == File.dirname(path)
      repository_path(File.dirname(path))
    end
  end
end

- (Object) repository_type



150
151
152
# File 'plugins/scm_git/lib/scm_git.rb', line 150

def repository_type
  "git"
end

- (Object) supported_commands



168
169
170
# File 'plugins/scm_git/lib/scm_git.rb', line 168

def supported_commands
  [:init, :commit, :index, :switch_branch, :push]
end

- (Object) switch!(branch)

REQUIRED for :switch_branch. Switches to the named branch.



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'plugins/scm_git/lib/scm_git.rb', line 452

def switch!(branch)
  if branch == 'New...'
    result = Application::Dialog.input("New Branch Name",
      "Please enter a new branch name","")
    if value = result[:value] and value != ""
      newbranch = value
      begin
        @repo.branch(newbranch).checkout
      rescue Object => e
        Application::Dialog.message_box(e.to_s)
        e.backtrace.each {|line| p line}
      end
    end
  else
    @repo.checkout(branch)
  end
  cache.refresh
end

- (Object) uncommited_changes

Not used by scm, but we do use this internally.



192
193
194
# File 'plugins/scm_git/lib/scm_git.rb', line 192

def uncommited_changes
  indexed_changes + unindexed_changes
end

- (Array<Redcar::Scm::ScmChangeMirror::Change>) unindexed_changes

Returns:

  • (Array<Redcar::Scm::ScmChangeMirror::Change>)


202
203
204
# File 'plugins/scm_git/lib/scm_git.rb', line 202

def unindexed_changes
  prepare_changes(false)
end

- (Array<Redcar::Scm::ScmMirror::Commit>) unpushed_commits(branch = current_branch)

REQUIRED for :push. Returns an array of unpushed changesets.

Returns:

  • (Array<Redcar::Scm::ScmMirror::Commit>)


500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'plugins/scm_git/lib/scm_git.rb', line 500

def unpushed_commits(branch=current_branch)
  # Hit `git config -l` to figure out which remote/ref this branch uses for pushing.
  remote = cache['config']['branch.' + branch + '.remote']
  push_target = cache['config']['branch.' + branch + '.push'] || cache['config']['branch.' + branch + '.merge']

  # We don't have a remote setup for pushes, so we can't automatically push
  return [] if remote.nil?

  # Hit .git/remotes/$REMOTE/$REF to find out which revision that ref is at.
  push_target.gsub!(/^refs\/heads\//, '')
  r_ref_file = File.join(@repo.dir.path, '.git', 'refs', 'remotes', remote, push_target)
  return [] if not File.exist?(r_ref_file)
  r_ref = File.read(r_ref_file).strip

  # Hit .git/refs/heads/$BRANCH to figure out which revision we're at locally.
  l_ref = File.read(File.join(@repo.dir.path, '.git', 'refs', 'heads', branch)).strip

  # Hit `git log $R_REV..$L_REV` to get a list of commits that are unpushed.
  if r_ref != l_ref
    cache['log', r_ref, l_ref].map {|c| Scm::Git::Commit.new(c)}
  else
    []
  end
end