active_git 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -18
- data/.travis.yml +7 -0
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +31 -29
- data/Rakefile +2 -2
- data/active_git.gemspec +25 -25
- data/lib/active_git.rb +47 -46
- data/lib/active_git/active_record_extension.rb +34 -49
- data/lib/active_git/commands.rb +146 -146
- data/lib/active_git/configuration.rb +28 -28
- data/lib/active_git/events/db_create.rb +8 -8
- data/lib/active_git/events/db_delete.rb +12 -12
- data/lib/active_git/events/db_delete_all.rb +15 -15
- data/lib/active_git/events/db_event.rb +28 -27
- data/lib/active_git/events/db_update.rb +14 -14
- data/lib/active_git/events/file_delete.rb +11 -11
- data/lib/active_git/events/file_event.rb +31 -31
- data/lib/active_git/events/file_save.rb +13 -13
- data/lib/active_git/events/folder_remove.rb +15 -15
- data/lib/active_git/inflector.rb +21 -0
- data/lib/active_git/synchronization_error.rb +32 -32
- data/lib/active_git/synchronizer.rb +53 -53
- data/lib/active_git/version.rb +3 -3
- data/spec/active_record_extension_spec.rb +60 -50
- data/spec/commands_spec.rb +245 -245
- data/spec/inflector_spec.rb +37 -0
- data/spec/migrations/20121004135939_create_countries.rb +9 -9
- data/spec/migrations/20121004135940_create_languages.rb +9 -9
- data/spec/migrations/20121030163114_create_brands.rb +9 -9
- data/spec/migrations/20130315192821_create_customers.rb +9 -0
- data/spec/spec_helper.rb +35 -23
- data/spec/support/helpers/file_helper.rb +47 -43
- data/spec/support/models/brand.rb +2 -4
- data/spec/support/models/country.rb +1 -2
- data/spec/support/models/customer.rb +6 -0
- data/spec/support/models/language.rb +2 -4
- data/spec/synchronization_spec.rb +100 -100
- metadata +56 -17
data/.gitignore
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
.bundle
|
4
|
-
.config
|
5
|
-
.yardoc
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
|
16
|
-
test/
|
17
|
-
|
18
|
-
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
spec/tmp
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
.idea
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in active_git.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in active_git.gemspec
|
4
|
+
gemspec
|
data/LICENSE
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
Copyright (c) 2012 gabriel
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
1
|
+
Copyright (c) 2012 gabriel
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
22
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
# ActiveGit
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
1
|
+
# ActiveGit
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/gabynaiman/active_git.png?branch=master)](https://travis-ci.org/gabynaiman/active_git)
|
4
|
+
|
5
|
+
DB and GIT synchronization via ActiveRecord and GitWrapper
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'active_git'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install active_git
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
require "bundler/gem_tasks"
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
data/active_git.gemspec
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/active_git/version', __FILE__)
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "active_git"
|
6
|
-
s.version = ActiveGit::VERSION
|
7
|
-
s.authors = ["Gabriel Naiman"]
|
8
|
-
s.email = ["gabynaiman@gmail.com"]
|
9
|
-
s.description = 'DB and GIT synchronization via ActiveRecord and GitWrapper'
|
10
|
-
s.summary = 'DB and GIT synchronization via ActiveRecord and GitWrapper'
|
11
|
-
s.homepage = 'https://github.com/gabynaiman/active_git'
|
12
|
-
|
13
|
-
s.files = `git ls-files`.split($\)
|
14
|
-
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
-
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
|
18
|
-
s.add_dependency 'activerecord', '>= 3.0.0'
|
19
|
-
s.add_dependency 'git_wrapper', '
|
20
|
-
s.add_dependency 'activerecord-import'
|
21
|
-
s.add_dependency 'easy_diff'
|
22
|
-
|
23
|
-
s.add_development_dependency 'rspec'
|
24
|
-
s.add_development_dependency 'sqlite3'
|
25
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_git/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "active_git"
|
6
|
+
s.version = ActiveGit::VERSION
|
7
|
+
s.authors = ["Gabriel Naiman"]
|
8
|
+
s.email = ["gabynaiman@gmail.com"]
|
9
|
+
s.description = 'DB and GIT synchronization via ActiveRecord and GitWrapper'
|
10
|
+
s.summary = 'DB and GIT synchronization via ActiveRecord and GitWrapper'
|
11
|
+
s.homepage = 'https://github.com/gabynaiman/active_git'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split($\)
|
14
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_dependency 'activerecord', '>= 3.0.0'
|
19
|
+
s.add_dependency 'git_wrapper', '~> 1.0'
|
20
|
+
s.add_dependency 'activerecord-import'
|
21
|
+
s.add_dependency 'easy_diff'
|
22
|
+
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'sqlite3'
|
25
|
+
end
|
data/lib/active_git.rb
CHANGED
@@ -1,46 +1,47 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'git_wrapper'
|
3
|
-
require 'activerecord-import'
|
4
|
-
require 'json'
|
5
|
-
require 'easy_diff'
|
6
|
-
|
7
|
-
require 'active_git/version'
|
8
|
-
require 'active_git/synchronizer'
|
9
|
-
require 'active_git/synchronization_error'
|
10
|
-
require 'active_git/events/db_event'
|
11
|
-
require 'active_git/events/db_create'
|
12
|
-
require 'active_git/events/db_update'
|
13
|
-
require 'active_git/events/db_delete'
|
14
|
-
require 'active_git/events/db_delete_all'
|
15
|
-
require 'active_git/events/file_event'
|
16
|
-
require 'active_git/events/file_save'
|
17
|
-
require 'active_git/events/file_delete'
|
18
|
-
require 'active_git/events/folder_remove'
|
19
|
-
require 'active_git/active_record_extension'
|
20
|
-
require 'active_git/configuration'
|
21
|
-
require 'active_git/commands'
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
@repository
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
1
|
+
require 'active_record'
|
2
|
+
require 'git_wrapper'
|
3
|
+
require 'activerecord-import'
|
4
|
+
require 'json'
|
5
|
+
require 'easy_diff'
|
6
|
+
|
7
|
+
require 'active_git/version'
|
8
|
+
require 'active_git/synchronizer'
|
9
|
+
require 'active_git/synchronization_error'
|
10
|
+
require 'active_git/events/db_event'
|
11
|
+
require 'active_git/events/db_create'
|
12
|
+
require 'active_git/events/db_update'
|
13
|
+
require 'active_git/events/db_delete'
|
14
|
+
require 'active_git/events/db_delete_all'
|
15
|
+
require 'active_git/events/file_event'
|
16
|
+
require 'active_git/events/file_save'
|
17
|
+
require 'active_git/events/file_delete'
|
18
|
+
require 'active_git/events/folder_remove'
|
19
|
+
require 'active_git/active_record_extension'
|
20
|
+
require 'active_git/configuration'
|
21
|
+
require 'active_git/commands'
|
22
|
+
require 'active_git/inflector'
|
23
|
+
|
24
|
+
module ActiveGit
|
25
|
+
extend Commands
|
26
|
+
|
27
|
+
def self.configuration
|
28
|
+
@@configuration ||= Configuration.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.configure
|
32
|
+
yield(configuration)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.models
|
36
|
+
@@models ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.repository
|
40
|
+
@repository = GitWrapper::Repository.new(ActiveGit.configuration.working_path) if @repository.nil? || @repository.location != ActiveGit.configuration.working_path
|
41
|
+
@repository
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
ActiveRecord::Base.send :extend, ActiveGit::ActiveRecord::ClassMethods
|
47
|
+
|
@@ -1,50 +1,35 @@
|
|
1
|
-
module ActiveGit
|
2
|
-
module ActiveRecord
|
3
|
-
|
4
|
-
module ClassMethods
|
5
|
-
|
6
|
-
def git_versioned
|
7
|
-
ActiveGit.models << self
|
8
|
-
|
9
|
-
after_save do |record|
|
10
|
-
Synchronizer.synchronize FileSave.new(record)
|
11
|
-
end
|
12
|
-
|
13
|
-
after_destroy do |record|
|
14
|
-
Synchronizer.synchronize FileDelete.new(record)
|
15
|
-
end
|
16
|
-
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
include InstanceMethods
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
module InstanceMethods
|
42
|
-
|
43
|
-
def git_file
|
44
|
-
"#{self.class.git_folder}/#{id}.json"
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
1
|
+
module ActiveGit
|
2
|
+
module ActiveRecord
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
|
6
|
+
def git_versioned
|
7
|
+
ActiveGit.models << self
|
8
|
+
|
9
|
+
after_save do |record|
|
10
|
+
Synchronizer.synchronize FileSave.new(record)
|
11
|
+
end
|
12
|
+
|
13
|
+
after_destroy do |record|
|
14
|
+
Synchronizer.synchronize FileDelete.new(record)
|
15
|
+
end
|
16
|
+
|
17
|
+
def from_json(json)
|
18
|
+
record = self.new
|
19
|
+
hash = json.is_a?(Hash) ? json : JSON.parse(json)
|
20
|
+
hash.each do |attr, value|
|
21
|
+
if record.respond_to? "#{attr}="
|
22
|
+
if self.columns_hash[attr].type == :datetime
|
23
|
+
record.send("#{attr}=", Time.parse(value).utc)
|
24
|
+
else
|
25
|
+
record.send("#{attr}=", value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
record
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
50
35
|
end
|
data/lib/active_git/commands.rb
CHANGED
@@ -1,147 +1,147 @@
|
|
1
|
-
module ActiveGit
|
2
|
-
module Commands
|
3
|
-
|
4
|
-
GitWrapper::Repository.instance_methods(false).each do |method|
|
5
|
-
define_method method do |*args, &block|
|
6
|
-
repository.send method, *args, &block
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def dump_db(*models)
|
11
|
-
events = (Dir["#{ActiveGit.configuration.working_path}/*"] - ActiveGit.models.map(
|
12
|
-
FolderRemove.new(folder)
|
13
|
-
end
|
14
|
-
|
15
|
-
(models.any? ? models : ActiveGit.models).each do |model|
|
16
|
-
events << FolderRemove.new(model
|
17
|
-
events = events + model.all.map { |r| FileSave.new r }
|
18
|
-
end
|
19
|
-
|
20
|
-
Synchronizer.synchronize events
|
21
|
-
end
|
22
|
-
|
23
|
-
def load_files(*models)
|
24
|
-
events = []
|
25
|
-
|
26
|
-
(models.any? ? models : ActiveGit.models).each do |model|
|
27
|
-
events << DbDeleteAll.new(model)
|
28
|
-
Dir.glob("#{model
|
29
|
-
events << DbCreate.new(file_name)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
Synchronizer.synchronize events
|
34
|
-
end
|
35
|
-
|
36
|
-
def commit_all(message, options={})
|
37
|
-
add_all
|
38
|
-
commit(message, options)
|
39
|
-
end
|
40
|
-
|
41
|
-
def pull(remote='origin', branch='master')
|
42
|
-
fetch remote
|
43
|
-
merge "#{remote}/#{branch}"
|
44
|
-
end
|
45
|
-
|
46
|
-
def merge(commit)
|
47
|
-
last_log = log.first
|
48
|
-
diffs = diff_reverse commit unless last_log
|
49
|
-
|
50
|
-
unless repository.merge(commit)
|
51
|
-
resolve_conflicts
|
52
|
-
diffs = diff 'HEAD'
|
53
|
-
commit_all 'Resolve conflicts'
|
54
|
-
end
|
55
|
-
|
56
|
-
diffs ||= repository.diff(last_log.commit_hash)
|
57
|
-
begin
|
58
|
-
synchronize_diffs diffs
|
59
|
-
rescue => e
|
60
|
-
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
61
|
-
reset last_log.commit_hash
|
62
|
-
return false
|
63
|
-
end
|
64
|
-
|
65
|
-
true
|
66
|
-
end
|
67
|
-
|
68
|
-
def conflicts
|
69
|
-
status.select { |e| e.status == :merge_conflict }.map { |e| e.file_name }
|
70
|
-
end
|
71
|
-
|
72
|
-
def resolve_conflicts
|
73
|
-
json_parse = Proc.new do |text|
|
74
|
-
text.present? ? JSON.parse(text) : {}
|
75
|
-
end
|
76
|
-
|
77
|
-
events = conflicts.map do |file_name|
|
78
|
-
base = json_parse.call(show_base(file_name))
|
79
|
-
mine = json_parse.call(show_mine(file_name))
|
80
|
-
theirs = json_parse.call(show_theirs(file_name))
|
81
|
-
|
82
|
-
r_diff, a_diff = base.easy_diff(mine)
|
83
|
-
merge = theirs.easy_unmerge(r_diff).easy_merge(a_diff)
|
84
|
-
|
85
|
-
model = File.dirname(file_name).split(/\/|\\/).pop.classify.constantize
|
86
|
-
|
87
|
-
FileSave.new(model.from_json(merge))
|
88
|
-
end
|
89
|
-
|
90
|
-
Synchronizer.synchronize events
|
91
|
-
end
|
92
|
-
|
93
|
-
def checkout(commit, new_branch=nil)
|
94
|
-
current = current_branch
|
95
|
-
diffs = repository.diff_reverse commit
|
96
|
-
if repository.checkout(commit.split('/').last, new_branch)
|
97
|
-
begin
|
98
|
-
synchronize_diffs diffs
|
99
|
-
rescue SynchronizationError => e
|
100
|
-
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
101
|
-
repository.checkout current
|
102
|
-
return false
|
103
|
-
end
|
104
|
-
true
|
105
|
-
else
|
106
|
-
false
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def reset(commit='HEAD')
|
111
|
-
diffs = diff_reverse commit
|
112
|
-
if repository.reset mode: :hard, commit: commit
|
113
|
-
begin
|
114
|
-
synchronize_diffs diffs
|
115
|
-
rescue SynchronizationError => e
|
116
|
-
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
117
|
-
#TODO: Rollback reset
|
118
|
-
return false
|
119
|
-
end
|
120
|
-
true
|
121
|
-
else
|
122
|
-
false
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
private
|
127
|
-
|
128
|
-
def synchronize_diffs(diffs)
|
129
|
-
events = diffs.map do |d|
|
130
|
-
file_name = "#{location}/#{d.file_name}"
|
131
|
-
|
132
|
-
if d.status == :new_file
|
133
|
-
DbCreate.new file_name
|
134
|
-
elsif [:modified, :renamed, :copied].include? d.status
|
135
|
-
DbUpdate.new file_name
|
136
|
-
elsif d.status == :deleted
|
137
|
-
DbDelete.new file_name
|
138
|
-
else
|
139
|
-
raise "Unexpected file status [#{d.status}]"
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
Synchronizer.synchronize events
|
144
|
-
end
|
145
|
-
|
146
|
-
end
|
1
|
+
module ActiveGit
|
2
|
+
module Commands
|
3
|
+
|
4
|
+
GitWrapper::Repository.instance_methods(false).each do |method|
|
5
|
+
define_method method do |*args, &block|
|
6
|
+
repository.send method, *args, &block
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def dump_db(*models)
|
11
|
+
events = (Dir["#{ActiveGit.configuration.working_path}/*"] - ActiveGit.models.map { |m| Inflector.dirname(m) }).map do |folder|
|
12
|
+
FolderRemove.new(folder)
|
13
|
+
end
|
14
|
+
|
15
|
+
(models.any? ? models : ActiveGit.models).each do |model|
|
16
|
+
events << FolderRemove.new(Inflector.dirname(model))
|
17
|
+
events = events + model.all.map { |r| FileSave.new r }
|
18
|
+
end
|
19
|
+
|
20
|
+
Synchronizer.synchronize events
|
21
|
+
end
|
22
|
+
|
23
|
+
def load_files(*models)
|
24
|
+
events = []
|
25
|
+
|
26
|
+
(models.any? ? models : ActiveGit.models).each do |model|
|
27
|
+
events << DbDeleteAll.new(model)
|
28
|
+
Dir.glob("#{Inflector.dirname(model)}/*.json").each do |file_name|
|
29
|
+
events << DbCreate.new(file_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Synchronizer.synchronize events
|
34
|
+
end
|
35
|
+
|
36
|
+
def commit_all(message, options={})
|
37
|
+
add_all
|
38
|
+
commit(message, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def pull(remote='origin', branch='master')
|
42
|
+
fetch remote
|
43
|
+
merge "#{remote}/#{branch}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def merge(commit)
|
47
|
+
last_log = log.first
|
48
|
+
diffs = diff_reverse commit unless last_log
|
49
|
+
|
50
|
+
unless repository.merge(commit)
|
51
|
+
resolve_conflicts
|
52
|
+
diffs = diff 'HEAD'
|
53
|
+
commit_all 'Resolve conflicts'
|
54
|
+
end
|
55
|
+
|
56
|
+
diffs ||= repository.diff(last_log.commit_hash)
|
57
|
+
begin
|
58
|
+
synchronize_diffs diffs
|
59
|
+
rescue => e
|
60
|
+
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
61
|
+
reset last_log.commit_hash
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
|
65
|
+
true
|
66
|
+
end
|
67
|
+
|
68
|
+
def conflicts
|
69
|
+
status.select { |e| e.status == :merge_conflict }.map { |e| e.file_name }
|
70
|
+
end
|
71
|
+
|
72
|
+
def resolve_conflicts
|
73
|
+
json_parse = Proc.new do |text|
|
74
|
+
text.present? ? JSON.parse(text) : {}
|
75
|
+
end
|
76
|
+
|
77
|
+
events = conflicts.map do |file_name|
|
78
|
+
base = json_parse.call(show_base(file_name))
|
79
|
+
mine = json_parse.call(show_mine(file_name))
|
80
|
+
theirs = json_parse.call(show_theirs(file_name))
|
81
|
+
|
82
|
+
r_diff, a_diff = base.easy_diff(mine)
|
83
|
+
merge = theirs.easy_unmerge(r_diff).easy_merge(a_diff)
|
84
|
+
|
85
|
+
model = File.dirname(file_name).split(/\/|\\/).pop.classify.constantize
|
86
|
+
|
87
|
+
FileSave.new(model.from_json(merge))
|
88
|
+
end
|
89
|
+
|
90
|
+
Synchronizer.synchronize events
|
91
|
+
end
|
92
|
+
|
93
|
+
def checkout(commit, new_branch=nil)
|
94
|
+
current = current_branch
|
95
|
+
diffs = repository.diff_reverse commit
|
96
|
+
if repository.checkout(commit.split('/').last, new_branch)
|
97
|
+
begin
|
98
|
+
synchronize_diffs diffs
|
99
|
+
rescue SynchronizationError => e
|
100
|
+
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
101
|
+
repository.checkout current
|
102
|
+
return false
|
103
|
+
end
|
104
|
+
true
|
105
|
+
else
|
106
|
+
false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def reset(commit='HEAD')
|
111
|
+
diffs = diff_reverse commit
|
112
|
+
if repository.reset mode: :hard, commit: commit
|
113
|
+
begin
|
114
|
+
synchronize_diffs diffs
|
115
|
+
rescue SynchronizationError => e
|
116
|
+
ActiveGit.configuration.logger.error "[ActiveGit] #{e}"
|
117
|
+
#TODO: Rollback reset
|
118
|
+
return false
|
119
|
+
end
|
120
|
+
true
|
121
|
+
else
|
122
|
+
false
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def synchronize_diffs(diffs)
|
129
|
+
events = diffs.map do |d|
|
130
|
+
file_name = "#{location}/#{d.file_name}"
|
131
|
+
|
132
|
+
if d.status == :new_file
|
133
|
+
DbCreate.new file_name
|
134
|
+
elsif [:modified, :renamed, :copied].include? d.status
|
135
|
+
DbUpdate.new file_name
|
136
|
+
elsif d.status == :deleted
|
137
|
+
DbDelete.new file_name
|
138
|
+
else
|
139
|
+
raise "Unexpected file status [#{d.status}]"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
Synchronizer.synchronize events
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
147
|
end
|