gitdb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16dbf12f4855f55ed5e8f9209eb83f4653887555
4
+ data.tar.gz: bb5615996d5d27ac557de62703f282a6bc3db22f
5
+ SHA512:
6
+ metadata.gz: d58004d7860d29bf559062a1cd58df5c4192d6ba6dcc573ab4d64dec2a1b3b539521f3f21c295d93e902976832d532e5aade51a6bf54bbec026b903b43066b31
7
+ data.tar.gz: bb9840bc0e5271fe8c3fdb70ea958720090f55952f7bb0e6d207d4891d61cf63f8ccca1ddc417c293403390607b65e1f5296744573f11eda40743fc1799cc3a2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gitdb.gemspec
4
+ rugged
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ran
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
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Gitdb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gitdb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gitdb
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/gitdb/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/gitdb.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gitdb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gitdb"
8
+ spec.version = Gitdb::VERSION
9
+ spec.authors = ["Ran"]
10
+ spec.email = ["abbshr@outlook.com"]
11
+ spec.summary = %q{"Git-Contacts backend data engine"}
12
+ spec.description = %q{"a simple data storage based on git, designed for Git-Contacts"}
13
+ spec.homepage = "https://github.com/AustinChou/Git-Contacts"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
data/lib/gitdb/Card.rb ADDED
@@ -0,0 +1,120 @@
1
+
2
+ module Gitdb
3
+ class Card
4
+
5
+ def initialize repo
6
+ @repo = repo
7
+ end
8
+
9
+ def self::exist? repo, id
10
+ repo.head.target.tree.find { |o| o[:name] == id } unless repo.branches.count == 0
11
+ end
12
+
13
+ def exist? id
14
+ Card::exist? @repo, id
15
+ end
16
+
17
+ # 设置名片默认格式
18
+ # Notice: 每个hash的key为Symbol类型
19
+ def format_card id, uid
20
+ {
21
+ meta: {
22
+ id: id,
23
+ owner: uid
24
+ },
25
+ firstname: '',
26
+ lastname: '',
27
+ mobile: [],
28
+ phone: [],
29
+ email: [],
30
+ address: [],
31
+ im: [],
32
+ birthday: '',
33
+ note: ''
34
+ }
35
+ end
36
+
37
+ def create uid
38
+ # 生成唯一的名片id
39
+ while exist?(@id = Gitil::generate_code(4))
40
+ end
41
+ # 记录名片创建者id
42
+ @uid = uid
43
+ # 用默认格式初始化名片可读内容
44
+ @content = format_card @id, @uid
45
+ # 返回Card实例
46
+ self
47
+ end
48
+
49
+ def access id
50
+ @id = id
51
+ o = @repo.head.target.tree.find do |o|
52
+ o if o[:name] == id
53
+ end
54
+ # 找到名片后加载名片数据, 并返回Card实例, 否则返回nil
55
+ if o != nil
56
+ @content = JSON.parse @repo.lookup(o[:oid]).content, { symbolize_names: true }
57
+ @uid = @content[:owner]
58
+ self
59
+ else
60
+ nil
61
+ end
62
+ end
63
+
64
+ def self::getdata repo, id
65
+ Card.new(repo).access(id).getdata
66
+ end
67
+
68
+ def self::setdata repo, id, hash
69
+ Card.new(repo).access(id).setdata hash
70
+ end
71
+
72
+ def self::getmeta repo, id
73
+ Card.new(repo).access(id).getmeta
74
+ end
75
+
76
+ def self::setmeta repo, id, hash
77
+ Card.new(repo).access(id).setmeta hash
78
+ end
79
+
80
+ def self::delete repo, id
81
+ Card.new(repo).access(id).delete
82
+ end
83
+
84
+ def getdata
85
+ data = @content.clone
86
+ data.delete :meta
87
+ data
88
+ end
89
+
90
+ def setdata hash
91
+ h = Gitil::data_keys_of_card.map { |key| key.to_sym } & hash.keys
92
+ h.each do |sym_key|
93
+ @content[sym_key] = hash[sym_key]
94
+ end
95
+ # 每次对数据的修改会触发一次"写入暂存区"
96
+ write_to_stage @id, JSON.pretty_generate(@content)
97
+ end
98
+
99
+ def getmeta
100
+ @content.clone[:meta]
101
+ end
102
+
103
+ def setmeta hash
104
+ @content[:meta] = hash
105
+ end
106
+
107
+ # Notice: 调用delete之后需要先commit, 然后才能继续调用write_to_stage
108
+ def delete
109
+ @repo.index.read_tree @repo.head.target.tree unless @repo.branches.count == 0
110
+ @repo.index.find { |blob| @repo.index.remove blob[:path] if blob[:path] == @id }
111
+ end
112
+
113
+ def write_to_stage id, content
114
+ oid = @repo.write content, :blob
115
+ @repo.index.read_tree @repo.head.target.tree unless @repo.branches.count == 0
116
+ @repo.index.add :path => id, :oid => oid, :mode => 0100644
117
+ end
118
+ end
119
+
120
+ end
@@ -0,0 +1,119 @@
1
+
2
+ module Gitdb
3
+ class Contacts
4
+
5
+ attr_accessor 'repo'
6
+
7
+ def initialize uid
8
+ @uid = uid
9
+ end
10
+
11
+ def self::exist? gid
12
+ Dir::exist? "#{STORAGE_PATH}/#{gid}"
13
+ end
14
+
15
+ def exist? gid
16
+ Contacts::exist? gid
17
+ end
18
+
19
+ def create name
20
+ # 生成联系人的唯一编码
21
+ while exist?(@gid = Gitil::generate_code(4))
22
+ end
23
+ # 创建并打开git仓库
24
+ @repo = Rugged::Repository.init_at "#{STORAGE_PATH}/#{@gid}"
25
+ # 设置元信息
26
+ setmeta :name => name, :gid => @gid, :uid => @uid
27
+ # 返回Contacts实例
28
+ self
29
+ end
30
+
31
+ def access gid
32
+ if exist?(@gid = gid)
33
+ @repo = Rugged::Repository.new "#{STORAGE_PATH}/#{gid}"
34
+ self
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ # 返回Card实例集合
41
+ def get_all_cards
42
+ if @repo.branches.count == 0
43
+ []
44
+ else
45
+ @repo.head.target.tree.collect.each { |o| Card.new(@repo).access o[:name] }
46
+ end
47
+ end
48
+
49
+ # 返回Card实例
50
+ def get_card_by_id id
51
+ if @repo.branches.count == 0
52
+ nil
53
+ else
54
+ Card.new(@repo).access id
55
+ end
56
+ end
57
+
58
+ def getmeta
59
+ @meta = {
60
+ :name => @repo.config['repo.name'],
61
+ :uid => @repo.config['repo.owner'],
62
+ :gid => @repo.config['repo.gid']
63
+ }
64
+ end
65
+
66
+ def setmeta hash
67
+ @repo.config['repo.name'] = hash[:name] if hash.member? :name
68
+ @repo.config['repo.owner'] = hash[:owner] if hash.member? :owner
69
+ @repo.config['repo.gid'] = hash[:gid] if hash.member? :gid
70
+ end
71
+
72
+ # 返回commit对象指向的tree-oid集合
73
+ def read_change_history
74
+ if @repo.branches.count == 0
75
+ []
76
+ else
77
+ walker = Rugged::Walker.new repo
78
+ walker.push repo.last_commit
79
+ walker.collect.each { |commit| commit.tree.oid }
80
+ end
81
+ end
82
+
83
+ # 等同于git工具的 "Revert"操作
84
+ # @sha: tree oid, NOT a commit oid
85
+ # 因为回滚需要的是tree而不是commit
86
+ # => commit.tree.oid
87
+ def revert_to sha, author, message
88
+ if @repo.branches.count == 0
89
+ return nil
90
+ end
91
+
92
+ tree = @repo.lookup sha
93
+ tree.each do |e|
94
+ # 遍历sha tree, 重新构造tree并写入暂存区
95
+ write_to_stage e[:name], @repo.lookup(e[:oid]).content
96
+ end
97
+ # get committer info
98
+ # committer = get_card_by_id(@uid).getdata.sub_hash(:name, :email).merge :time => Time.now
99
+ # 提交暂存区的tree
100
+ make_a_commit :author => author, :message => message, :committer => author
101
+ end
102
+
103
+ private
104
+ def write_to_stage card_id, content
105
+ oid = @repo.write content, :blob
106
+ @repo.index.add :path => card_id, :oid => oid, :mode => 0100644
107
+ end
108
+
109
+ # 生成一个commit对象
110
+ # 每次修改最终要调用
111
+ def make_a_commit options
112
+ options[:tree] = @repo.index.write_tree @repo
113
+ options[:parents] = @repo.empty? ? [] : [@repo.head.target].compact
114
+ options[:update_ref] = 'HEAD'
115
+ Rugged::Commit.create @repo, options
116
+ end
117
+ end
118
+
119
+ end
data/lib/gitdb/util.rb ADDED
@@ -0,0 +1,24 @@
1
+ module Gitil
2
+
3
+ def self::generate_code n
4
+ [*'a'..'z', *0..9, *'A'..'Z'].sample(n).join
5
+ end
6
+
7
+ def self::data_keys_of_card
8
+ ["firstname", "lastname", "mobile", "phone", "email", "birthday", "address", "im"]
9
+ end
10
+
11
+ def self::meta_keys_of_card
12
+ ["id", "owner"]
13
+ end
14
+
15
+ class Hash
16
+ def sub_hash *args
17
+ tmp = {}
18
+ args.each do |k|
19
+ tmp[k] = self[k] if self.has_key? k
20
+ end
21
+ tmp
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Gitdb
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gitdb.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "json"
2
+ require "rugged"
3
+ require_relative "gitdb/util"
4
+ require_relative "gitdb/version"
5
+
6
+ require_relative 'gitdb/Card'
7
+ require_relative 'gitdb/Contacts'
8
+
9
+ module Gitdb
10
+
11
+ # 主存储目录
12
+ STORAGE_PATH = File::expand_path '../../../storage', __FILE__
13
+
14
+ # 检查并创建主存储目录
15
+ def self::setup_storage
16
+ Dir::mkdir STORAGE_PATH unless Dir::exist? STORAGE_PATH
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: '"a simple data storage based on git, designed for Git-Contacts"'
42
+ email:
43
+ - abbshr@outlook.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - gitdb.gemspec
54
+ - lib/gitdb.rb
55
+ - lib/gitdb/Card.rb
56
+ - lib/gitdb/Contacts.rb
57
+ - lib/gitdb/util.rb
58
+ - lib/gitdb/version.rb
59
+ homepage: https://github.com/AustinChou/Git-Contacts
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: '"Git-Contacts backend data engine"'
83
+ test_files: []