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



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



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



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

def exist? gid
  Contacts::exist? gid
end

#get_all_cardsObject

返回Card实例集合



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

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实例



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

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

#getmetaObject



70
71
72
73
74
75
76
# File 'lib/gitdb/Contacts.rb', line 70

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对象每次修改最终要调用}



121
122
123
124
125
126
127
128
129
130
# File 'lib/gitdb/Contacts.rb', line 121

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



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

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”操作}



103
104
105
106
107
108
109
110
111
# File 'lib/gitdb/Contacts.rb', line 103

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



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

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