Class: GitHack::CommitLineBuilder

Inherits:
LineBuilder show all
Defined in:
lib/git-hack/commit_line_builder.rb

Instance Attribute Summary collapse

Attributes inherited from LineBuilder

#data, #index

Instance Method Summary collapse

Methods inherited from LineBuilder

#all_objects, #find_all, #get_next, #in?, #is_over?, #object, #parse, #rest

Constructor Details

#initialize(data, index) ⇒ CommitLineBuilder

Returns a new instance of CommitLineBuilder.



21
22
23
24
25
26
# File 'lib/git-hack/commit_line_builder.rb', line 21

def initialize(data,index)
	super(data,index)
	@is_message = false
	@commit = { 'sha'=>nil, 'message' => '', 'parent' => [] }
	@is_next_commit = false
end

Instance Attribute Details

#commitObject

Returns the value of attribute commit.



20
21
22
# File 'lib/git-hack/commit_line_builder.rb', line 20

def commit
  @commit
end

Instance Method Details

#line_to_commit(line) ⇒ Object

将每行的数据信息转换成Commit的信息



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git-hack/commit_line_builder.rb', line 40

def line_to_commit(line)
	data = line.split
	key = data.shift
	value = data.join(" ")

	if key == "commit"
	 	if @commit['sha']  		# key第二次是commit时 设置是否下条信息的标志,见 #out?
			@is_next_commit = true  
		else
			@commit['sha'] = value
		end
	elsif key == 'parent'
		@commit[key] << value
	else
		@commit[key] = value
	end
end

#out?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/git-hack/commit_line_builder.rb', line 57

def out?
	# 如果已经到下一条commit,则为结束标志,并指针并不再移动
	@index += 1 unless @is_next_commit
	return @is_next_commit 
end

#process_lineObject

重写#process_line



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/git-hack/commit_line_builder.rb', line 28

def process_line
	line = @data[@index].chomp.uncolorize
	if line == ""          # msg信息以上下两个"" 分隔
		@is_message = !@is_message
	elsif @is_message
		@commit['message'] << line+"\n"
	else
		line_to_commit(line)
	end
	@object = @commit.to_class(Commit)
end