module ActiveGit::Commands

Public Instance Methods

checkout(commit, new_branch=nil) click to toggle source
# File lib/active_git/commands.rb, line 92
def checkout(commit, new_branch=nil)
  current = current_branch
  diffs = repository.diff_reverse commit
  if repository.checkout(commit.split('/').last, new_branch)
    begin
      synchronize_diffs diffs
    rescue SynchronizationError => e
      ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
      repository.checkout current
      return false
    end
    true
  else
    false
  end
end
commit_all(message, options={}) click to toggle source
# File lib/active_git/commands.rb, line 36
def commit_all(message, options={})
  add_all
  commit(message, options)
end
conflicts() click to toggle source
# File lib/active_git/commands.rb, line 67
def conflicts
  status.select { |e| e.status == :merge_conflict }.map { |e| e.file_name }
end
dump_db(*models) click to toggle source
# File lib/active_git/commands.rb, line 10
def dump_db(*models)
  events = (Dir["#{ActiveGit.configuration.working_path}/*"] - ActiveGit.models.map { |m| Inflector.dirname(m) }).map do |folder|
    FolderRemove.new(folder)
  end

  (models.any? ? models : ActiveGit.models).each do |model|
    events << FolderRemove.new(Inflector.dirname(model))
    events = events + model.all.map { |r| FileSave.new r }
  end

  Synchronizer.synchronize events
end
load_files(*models) click to toggle source
# File lib/active_git/commands.rb, line 23
def load_files(*models)
  events = []

  (models.any? ? models : ActiveGit.models).each do |model|
    events << DbDeleteAll.new(model)
    Dir.glob("#{Inflector.dirname(model)}/*.json").each do |file_name|
      events << DbCreate.new(file_name)
    end
  end

  Synchronizer.synchronize events
end
merge(commit) click to toggle source
# File lib/active_git/commands.rb, line 46
def merge(commit)
  last_log = (log || []).first
  diffs = diff_reverse commit unless last_log

  unless repository.merge(commit)
    resolve_conflicts
    commit_all 'Resolve conflicts'
  end

  diffs ||= repository.diff("#{last_log.commit_hash}..HEAD")
  begin
    synchronize_diffs diffs
  rescue => e
    ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
    repository.reset mode: :hard, commit: last_log.commit_hash || 'HEAD'
    return false
  end

  true
end
pull(remote='origin', branch='master') click to toggle source
# File lib/active_git/commands.rb, line 41
def pull(remote='origin', branch='master')
  fetch remote
  merge "#{remote}/#{branch}"
end
reset(commit='HEAD') click to toggle source
# File lib/active_git/commands.rb, line 109
def reset(commit='HEAD')
  diffs = diff_reverse commit
  if repository.reset mode: :hard, commit: commit
    begin
      synchronize_diffs diffs
    rescue SynchronizationError => e
      ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
      #TODO: Rollback reset
      return false
    end
    true
  else
    false
  end
end
resolve_conflicts() click to toggle source
# File lib/active_git/commands.rb, line 71
def resolve_conflicts
  json_parse = Proc.new do |text|
    text.present? ? JSON.parse(text) : {}
  end

  events = conflicts.map do |file_name|
    base = json_parse.call(show_base(file_name))
    mine = json_parse.call(show_mine(file_name))
    theirs = json_parse.call(show_theirs(file_name))

    r_diff, a_diff = base.easy_diff(mine)
    merge = theirs.easy_unmerge(r_diff).easy_merge(a_diff)

    model = File.dirname(file_name).split(/\/|\\/).pop.classify.constantize

    FileSave.new(ModelParser.from_json(model, merge))
  end

  Synchronizer.synchronize events
end

Private Instance Methods

synchronize_diffs(diffs) click to toggle source
# File lib/active_git/commands.rb, line 127
def synchronize_diffs(diffs)
  events = diffs.map do |d|
    file_name = "#{location}/#{d.file_name}"

    if d.status == :new_file
      DbCreate.new file_name
    elsif [:modified, :renamed, :copied].include? d.status
      DbUpdate.new file_name
    elsif d.status == :deleted
      DbDelete.new file_name
    else
      raise "Unexpected file status [#{d.status}]"
    end
  end

  Synchronizer.synchronize events
end