Module: ActiveGit::Commands

Included in:
ActiveGit
Defined in:
lib/active_git/commands.rb

Instance Method Summary collapse

Instance Method Details

#checkout(commit, new_branch = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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 = {}) ⇒ Object



36
37
38
39
# File 'lib/active_git/commands.rb', line 36

def commit_all(message, options={})
  add_all
  commit(message, options)
end

#conflictsObject



67
68
69
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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') ⇒ Object



41
42
43
44
# File 'lib/active_git/commands.rb', line 41

def pull(remote='origin', branch='master')
  fetch remote
  merge "#{remote}/#{branch}"
end

#reset(commit = 'HEAD') ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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_conflictsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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