Class: Gitdb::Contacts

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

Constant Summary collapse

@@lock =

线程锁

false

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/gitdb/Contacts.rb', line 45

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
40
41
42
43
# File 'lib/gitdb/Contacts.rb', line 22

def create name
  # 等待资源可用
  loop do
    break unless @@lock
  end
  # 锁定资源
  @@lock = true

  # 生成联系人的唯一编码
  while exist?(@gid = Gitil::generate_code(4)) 
  end
  # 创建并打开git仓库
  @repo = Rugged::Repository.init_at "#{STORAGE_PATH}/#{@gid}"
  # 释放资源
  @@lock = false
  # 设置元信息
  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实例集合



57
58
59
60
61
62
63
# File 'lib/gitdb/Contacts.rb', line 57

def get_all_cards
  if @repo.head_unborn?
    []
  else
    @repo.head.target.tree.collect.each { |o| Card.new(@repo).access o[:name] }
  end
end

#get_card_by_id(id) ⇒ Object

返回Card实例



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

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

#getmetaObject



74
75
76
77
78
79
80
# File 'lib/gitdb/Contacts.rb', line 74

def getmeta
  @meta = {
    :name => @repo.config['repo.name'],
    :owner => @repo.config['repo.owner'],
    :gid => @repo.config['repo.gid']
  }
end

#make_a_commit(options) ⇒ Object

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



125
126
127
128
129
130
131
132
133
134
# File 'lib/gitdb/Contacts.rb', line 125

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_historyObject

返回commit对象oid



89
90
91
92
93
94
95
96
97
# File 'lib/gitdb/Contacts.rb', line 89

def read_change_history
  if @repo.head_unborn?
    []
  else
    walker = Rugged::Walker.new repo
    walker.push repo.last_commit
    walker.collect.each { |commit| commit.oid }
  end
end

#revert_to(sha, options) ⇒ Object

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



107
108
109
110
111
112
113
114
115
# File 'lib/gitdb/Contacts.rb', line 107

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



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

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