Class: Gitdb::Contacts

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdb/Contacts.rb

Constant Summary collapse

@@lock =

线程锁

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid) ⇒ Contacts

Returns a new instance of Contacts.



10
11
12
# File 'lib/gitdb/Contacts.rb', line 10

def initialize uid
  @uid = uid
end

Instance Attribute Details

#repoObject

Returns the value of attribute repo.



8
9
10
# File 'lib/gitdb/Contacts.rb', line 8

def repo
  @repo
end

Class Method Details

.exist?(gid) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/gitdb/Contacts.rb', line 14

def self::exist? gid
  Dir::exist? "#{STORAGE_PATH}/#{gid}"
end

Instance Method Details

#access(gid) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/gitdb/Contacts.rb', line 41

def access gid
  if exist?(@gid = gid)
    @repo = Rugged::Repository.new "#{STORAGE_PATH}/#{gid}"
    # 读取最后一次提交指向的tree的数据到暂存区
    @repo.index.read_tree @repo.last_commit.tree unless @repo.head_unborn?
    self
  else
    nil
  end
end

#create(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitdb/Contacts.rb', line 22

def create name

  # 同步信号量
  @@lock.synchronize {
    # 生成联系人的唯一编码
    while exist?(@gid = Gitil::generate_code(4)) 
    end
    # 创建并打开git仓库
    @repo = Rugged::Repository.init_at "#{STORAGE_PATH}/#{@gid}"
  }
  
  # 设置元信息
  setmeta :name => name, :gid => @gid, :owner => @uid
  # 读取最后一次提交指向的tree的数据到暂存区
  @repo.index.read_tree @repo.last_commit.tree unless @repo.head_unborn?
  # 返回Contacts实例
  self
end

#exist?(gid) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/gitdb/Contacts.rb', line 18

def exist? gid
  Contacts::exist? gid
end

#get_all_cardsObject

返回Card实例集合



74
75
76
# File 'lib/gitdb/Contacts.rb', line 74

def get_all_cards
  get_cards { |card| card }
end

#get_card_by_id(id) ⇒ Object

返回Card实例



53
54
55
56
57
58
59
# File 'lib/gitdb/Contacts.rb', line 53

def get_card_by_id id
  if @repo.head_unborn?
    nil
  else
    Card.new(@repo).access id
  end
end

#get_cards(&condition) ⇒ Object

条件查询 返回Card实例集合



63
64
65
66
67
68
69
70
71
# File 'lib/gitdb/Contacts.rb', line 63

def get_cards &condition
  if @repo.head_unborn?
    []
  else
    @repo.head.target.tree.map do |o|
      yield Card.new(@repo).access o[:name]
    end.compact
  end
end

#getmetaObject



78
79
80
81
82
83
84
85
86
# File 'lib/gitdb/Contacts.rb', line 78

def getmeta
  @meta = {
    :name => @repo.config['repo.name'],
    :owner => @repo.config['repo.owner'],
    :gid => @repo.config['repo.gid'],
    # 动态获取contacts中card数量
    :count => @repo.head_unborn? ? 0 : @repo.last_commit.tree.count
  }
end

#make_a_commit(options) ⇒ Object

生成一个commit对象 每次修改最终要调用 }



131
132
133
134
135
136
137
138
139
140
# File 'lib/gitdb/Contacts.rb', line 131

def make_a_commit options
  if @repo.index.count == 0 && @repo.head_unborn?
    return nil
  end
  options[:message] = 'make a commit' unless options.include? :message
  options[:tree] = @repo.index.write_tree @repo
  options[:parents] = @repo.empty? ? [] : [@repo.head.target].compact
  options[:update_ref] = 'HEAD'
  Rugged::Commit.create @repo, options
end

#read_change_history(&block) ⇒ Object

返回commit对象oid



95
96
97
98
99
100
101
102
103
# File 'lib/gitdb/Contacts.rb', line 95

def read_change_history &block
  if @repo.head_unborn?
    []
  else
    walker = Rugged::Walker.new repo
    walker.push repo.last_commit
    walker.map(&block).compact 
  end
end

#revert_to(sha, options) ⇒ Object

等同于git工具的 “Revert”操作 }



113
114
115
116
117
118
119
120
121
# File 'lib/gitdb/Contacts.rb', line 113

def revert_to sha, options
  return nil if @repo.head_unborn?
  commit = @repo.lookup sha
  tree = commit.tree
  # 构造tree
  @repo.index.read_tree tree
  # 提交暂存区的tree
  make_a_commit options
end

#setmeta(hash) ⇒ Object



88
89
90
91
92
# File 'lib/gitdb/Contacts.rb', line 88

def setmeta hash
  @repo.config['repo.name'] = hash[:name] if hash.member? :name
  @repo.config['repo.owner'] = hash[:owner] if hash.member? :owner
  @repo.config['repo.gid'] = hash[:gid] if hash.member? :gid
end