Class: GitHack::GitRepo

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

Overview

GitRepo类拥有git的所有 包括.git文件下所有的文件的功能的@git ,GitHack::Git类 work,即工作目录及文件 GitHack::WorkingDirectory类 remote,对应远程库信息 GitHack::Remote类

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathCommon

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

Constructor Details

#initialize(path) ⇒ GitRepo

Returns a new instance of GitRepo.



17
18
19
20
# File 'lib/git-hack/git_repo.rb', line 17

def initialize(path)
  @workingdirectory = get_gitdir(path) 
  @commits = []
end

Instance Attribute Details

#commitsObject

Returns the value of attribute commits.



16
17
18
# File 'lib/git-hack/git_repo.rb', line 16

def commits
  @commits
end

#gitObject

Returns the value of attribute git.



16
17
18
# File 'lib/git-hack/git_repo.rb', line 16

def git
  @git
end

#remoteObject

Returns the value of attribute remote.



16
17
18
# File 'lib/git-hack/git_repo.rb', line 16

def remote
  @remote
end

#workObject

Returns the value of attribute work.



16
17
18
# File 'lib/git-hack/git_repo.rb', line 16

def work
  @work
end

Instance Method Details

#add_workingdirectoryObject



56
57
58
# File 'lib/git-hack/git_repo.rb', line 56

def add_workingdirectory
  git.add(@workingdirectory) 
end

#auto_commit_msgObject



67
68
69
# File 'lib/git-hack/git_repo.rb', line 67

def auto_commit_msg
  "auto commit" # TODO: 需要完成
end

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

check out 出前第number个保存



76
77
78
79
80
81
82
83
84
85
# File 'lib/git-hack/git_repo.rb', line 76

def checkout(number,options={})
  ready_to_execute
  return self if not_git_directory?
  puts "commits:".colorize(:red)
  ap commits
  git.reset(commits[1])
  execute_success
  self
  
end

#commit(msg) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/git-hack/git_repo.rb', line 59

def commit(msg)
  msg ||= auto_commit_msg 
  begin
    git.commit(msg,:add_all=>true)  
  rescue Git::GitExecuteError => e
    puts e.to_s.colorize(:green)
  end
end

#execute_successObject



50
51
52
# File 'lib/git-hack/git_repo.rb', line 50

def execute_success
  @is_success = true
end

#get_gitdir(path) ⇒ Object

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



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

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仓库并提交



39
40
41
42
43
44
45
46
# File 'lib/git-hack/git_repo.rb', line 39

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

#init(dir) ⇒ Object



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

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

#is_gitdir?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_gitdir?(path)
  File.directory?(join(path,".git")) ? true : false
end

#not_git_directory?Boolean

Returns:

  • (Boolean)


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

def not_git_directory?
  if @workingdirectory == nil
    puts "Init first,run git init"
    return true
  end
  return false
end

#ready_to_executeObject



47
48
49
# File 'lib/git-hack/git_repo.rb', line 47

def ready_to_execute
  @is_success = false
end

#success?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/git-hack/git_repo.rb', line 53

def success?
  @is_success
end

#undoObject

undo 如果有修改,保存,然后回到上一次提交



71
72
73
74
# File 'lib/git-hack/git_repo.rb', line 71

def undo
  git_save if working_directory_change?
  checkout(1)    # check_out 0.当前1.上一个.2.上上个....
end