Class: Gitdb::Contacts

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid) ⇒ Contacts

Returns a new instance of Contacts.


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

def initialize uid
  @uid = uid
end

Instance Attribute Details

#repoObject

Returns the value of attribute repo.


5
6
7
# File 'lib/gitdb/Contacts.rb', line 5

def repo
  @repo
end

Class Method Details

.exist?(gid) ⇒ Boolean

Returns:

  • (Boolean)

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

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

Instance Method Details

#access(gid) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/gitdb/Contacts.rb', line 31

def access gid
  if exist?(@gid = gid)
    @repo = Rugged::Repository.new "#{STORAGE_PATH}/#{gid}"
    self
  else
    nil
  end
end

#create(name) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitdb/Contacts.rb', line 19

def create name
  # 生成联系人的唯一编码
  while exist?(@gid = Gitil::generate_code(4)) 
  end
  # 创建并打开git仓库
  @repo = Rugged::Repository.init_at "#{STORAGE_PATH}/#{@gid}"
  # 设置元信息
  setmeta :name => name, :gid => @gid, :uid => @uid
  # 返回Contacts实例
  self
end

#exist?(gid) ⇒ Boolean

Returns:

  • (Boolean)

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

def exist? gid
  Contacts::exist? gid
end

#get_all_cardsObject

返回Card实例集合


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

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

#get_card_by_id(id) ⇒ Object

返回Card实例


50
51
52
53
54
55
56
# File 'lib/gitdb/Contacts.rb', line 50

def get_card_by_id id
  if @repo.branches.count == 0
    nil
  else
    Card.new(@repo).access id
  end
end

#getmetaObject


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

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

#read_change_historyObject

返回commit对象指向的tree-oid集合


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

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

#revert_to(sha, author, message) ⇒ Object

等同于git工具的 “Revert”操作 @sha: tree oid, NOT a commit oid 因为回滚需要的是tree而不是commit

=> commit.tree.oid

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gitdb/Contacts.rb', line 87

def revert_to sha, author, message
  if @repo.branches.count == 0
    return nil
  end

  tree = @repo.lookup sha
  tree.each do |e|
    # 遍历sha tree, 重新构造tree并写入暂存区
    write_to_stage e[:name], @repo.lookup(e[:oid]).content
  end
  # get committer info
  # committer = get_card_by_id(@uid).getdata.sub_hash(:name, :email).merge :time => Time.now
  # 提交暂存区的tree
  make_a_commit :author => author, :message => message, :committer => author
end

#setmeta(hash) ⇒ Object


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

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