class Coaster::Git::Repository

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/coaster/git/repository.rb, line 8
def initialize(path)
  @path = path
end

Public Instance Methods

add(*paths, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 28
def add(*paths, **options)
  opts = Options.new('add', **options)
  run_git_cmd("add", *paths, opts)
end
branch(*args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 39
def branch(*args, **options)
  run_git_cmd("branch", *args, **options)
end
checkout(*args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 43
def checkout(*args, **options)
  run_git_cmd("checkout", *args, **options)
end
commit(*args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 33
def commit(*args, **options)
  opts = Options.new('commit', *args, **options)
  opts['--message'] ||= "no message"
  run_git_cmd("commit", opts)
end
current_sha() click to toggle source
# File lib/coaster/git/repository.rb, line 67
def current_sha
  run_git_cmd('rev-parse HEAD').strip
end
deep_merge(pointer) click to toggle source
# File lib/coaster/git/repository.rb, line 116
def deep_merge(pointer)
  puts "[DEEP_MERGE] #{path} #{pointer}"
  submodules.values.each do |submodule|
    sm_sha = submodule_sha(submodule.path, pointer: pointer)
    submodule.merge(sm_sha) if sm_sha.present?
  end
  merge_without_submodules do
    merge(pointer) if pointer.present?
  end
end
fetch(*args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 59
def fetch(*args, **options)
  run_git_cmd("fetch", *args, **options)
end
merge(pointer, *args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 83
def merge(pointer, *args, **options)
  opts = Options.new('merge', *args, **options)
  pointers = pointers(pointer).join(',')
  puts "[MERGE] #{path} #{pointers} #{options}"
  opts['--message'] ||= "Merge #{pointers}"
  run_git_cmd("merge #{pointer} #{opts}")
end
merge_without_submodules() { || ... } click to toggle source
# File lib/coaster/git/repository.rb, line 96
def merge_without_submodules
  run_git_cmd('config merge.ours.name "Keep ours merge driver"')
  run_git_cmd('config merge.ours.driver true')
  ga_file = File.join(@path, '.gitattributes')
  run_cmd("touch #{ga_file}")
  ga_lines = File.read(ga_file).split("\n")
  ga_lines_appended = ga_lines + submodules.keys.map{|sb_path| "#{sb_path} merge=ours" }
  File.open(ga_file, 'w') do |f|
    f.puts ga_lines_appended.join("\n")
  end
  add('.')
  commit
  yield
  File.open(ga_file, 'w') do |f|
    f.puts ga_lines.join("\n")
  end
  add('.')
  commit
end
pointers(sha) click to toggle source
# File lib/coaster/git/repository.rb, line 127
def pointers(sha)
  run_git_cmd("branch --contains #{sha}").split("\n").map do |br|
    br = (br.start_with?('*') ? br[2..-1] : br).strip
    br.match?(/^\(.*\)$/) ? nil : br
  end.compact
end
remove() click to toggle source
# File lib/coaster/git/repository.rb, line 134
def remove
  run_cmd("rm -rf #{path}", path: path.split('/')[0..-2].join('/'))
end
run_cmd(command, path: nil) click to toggle source
Calls superclass method Coaster::Git#run_cmd
# File lib/coaster/git/repository.rb, line 12
def run_cmd(command, path: nil)
  super(path || @path, command)
end
run_git_cmd(command, *args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 22
def run_git_cmd(command, *args, **options)
  opts = Options.new(command, *args, **options)
  cmd = "git #{@git_options} #{Array.wrap(command).join(' ')} #{opts}"
  run_cmd(cmd)
end
status(*args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 63
def status(*args, **options)
  run_git_cmd("status", *args, **options)
end
submodule_add!(repo, path, *args, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 47
def submodule_add!(repo, path, *args, **options)
  run_git_cmd(["submodule", 'add'], repo, path, *args, **options)
end
submodule_init!(*paths) click to toggle source
# File lib/coaster/git/repository.rb, line 51
def submodule_init!(*paths)
  run_git_cmd(["submodule", "init"], *paths)
end
submodule_paths() click to toggle source
# File lib/coaster/git/repository.rb, line 71
def submodule_paths
  @submodule_paths ||= run_git_cmd('submodule status --recursive').split("\n").map do |line|
    line.split(' ')[1]
  end
end
submodule_sha(path, pointer: nil) click to toggle source
# File lib/coaster/git/repository.rb, line 91
def submodule_sha(path, pointer: nil)
  pointer ||= current_sha
  run_git_cmd("ls-tree #{pointer} #{path}").split(' ')[2]
end
submodule_update!(*paths, **options) click to toggle source
# File lib/coaster/git/repository.rb, line 55
def submodule_update!(*paths, **options)
  run_git_cmd(["submodule", "update"], *paths, **options)
end
submodules() click to toggle source
# File lib/coaster/git/repository.rb, line 77
def submodules
  @submodules ||= submodule_paths.map do |path|
    [path, Git::Repository.new(File.join(@path, path))]
  end.to_h
end
with_git_options(*args, **options) { || ... } click to toggle source
# File lib/coaster/git/repository.rb, line 16
def with_git_options(*args, **options, &block)
  @git_options = Options.new('git', *args, **options)
  yield
  @git_options = nil
end