Class: GitHack::GitRepo

Inherits:
Git::Path
  • Object
show all
Includes:
PathCommon, SaveExecute
Defined in:
lib/git-hack/git_repo.rb

Overview

GitRepo类拥有git的所有 包括.git文件下所有的文件的功能的@git ,GitHack::Git类

work,即工作目录及文件 GitHack::WorkingDirectory类
remote,对应远程库信息 GitHack::Remote类

对外操作的函数:

# init(dir)
# goto(number)
# redo
# undo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SaveExecute

#execute_success, #ready_to_execute, #success?

Methods included from PathCommon

#absolute_path, #join, #project_dir, #project_file, #require_p

Constructor Details

#initialize(path) ⇒ GitRepo

Returns a new instance of GitRepo.



28
29
30
31
32
33
# File 'lib/git-hack/git_repo.rb', line 28

def initialize(path)
	@workingdirectory = get_gitdir(path)	
	@git = nil
	@commits = nil
	@commit_facade = nil
end

Instance Attribute Details

#commitsObject

Returns the value of attribute commits.



26
27
28
# File 'lib/git-hack/git_repo.rb', line 26

def commits
  @commits
end

#current_commitObject

Returns the value of attribute current_commit.



26
27
28
# File 'lib/git-hack/git_repo.rb', line 26

def current_commit
  @current_commit
end

#gitObject

属性函数



36
37
38
# File 'lib/git-hack/git_repo.rb', line 36

def git
  @git
end

#remoteObject

Returns the value of attribute remote.



26
27
28
# File 'lib/git-hack/git_repo.rb', line 26

def remote
  @remote
end

#workObject

Returns the value of attribute work.



26
27
28
# File 'lib/git-hack/git_repo.rb', line 26

def work
  @work
end

Instance Method Details

#commit_facadeObject



39
40
41
# File 'lib/git-hack/git_repo.rb', line 39

def commit_facade
	@commit_facade ||= CommitFacade.new(@workingdirectory)
end

#get_gitdir(path) ⇒ Object

得到本身或是上层目录中.git文件的路经



92
93
94
95
96
97
# File 'lib/git-hack/git_repo.rb', line 92

def get_gitdir(path)
	git_path = absolute_path(path)
	return nil if git_path == "/"
	return git_path if is_gitdir?(git_path) 
	return get_gitdir(join(git_path,"/../"))
end

#git_save(msg, options = {}) ⇒ Object

把工作目录的文件添加到git仓库并提交



48
49
50
51
52
53
54
55
# File 'lib/git-hack/git_repo.rb', line 48

def git_save(msg,options={})
	ready_to_execute
	return self if not_git_directory?
	add_workingdirectory
	commit(msg)
	execute_success
	self
end

#goto(number, options = {}) ⇒ Object

回到前第number个保存



73
74
75
76
77
78
79
80
# File 'lib/git-hack/git_repo.rb', line 73

def goto(number,options={})
	ready_to_execute
	return self if not_git_directory?
	ap commits
	git.reset_hard(commits[number].sha)
	execute_success
	self
end

#init(dir) ⇒ Object

初始化空文件夹成为git库



82
83
84
85
# File 'lib/git-hack/git_repo.rb', line 82

def init(dir)
	@git = Git.init(dir)
	@workingdirectory = dir
end

#redoObject

redo 到当前提交的下一个提交



62
63
64
65
66
67
68
69
70
# File 'lib/git-hack/git_repo.rb', line 62

def redo
	ready_to_execute
	return self if not_git_directory?
	next_commit = commit_facade.get_next_commit
	return self if !next_commit
	git.reset_hard(next_commit)
	execute_success
	self
end

#show_commitsObject



86
87
88
# File 'lib/git-hack/git_repo.rb', line 86

def show_commits
	ap commits
end

#undoObject

undo 回到上一次提交



58
59
60
# File 'lib/git-hack/git_repo.rb', line 58

def undo
	goto(1)
end