Class: Nabokov::GitRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/nabokov/git/git_repo.rb

Overview

This class is basically the wrapper aroung the ruby-git gem Note: the ruby gem is taken not from gem spec repo, but from github.com/Antondomashnev/ruby-git

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_path, remote_url = nil, git_repo = nil) ⇒ GitRepo

Returns a new instance of GitRepo.



17
18
19
20
21
22
23
24
# File 'lib/nabokov/git/git_repo.rb', line 17

def initialize(local_path, remote_url = nil, git_repo = nil)
  raise "local_path is a required parameter" if local_path.nil?
  expanded_local_path = File.expand_path(local_path)
  @local_pathname = Pathname.new(expanded_local_path)
  @git_repo = git_repo
  self.remote_url = remote_url
  self.local_path = expanded_local_path
end

Instance Attribute Details

#git_repoGit::Base (readonly)

Returns Underlying git repo.

Returns:

  • (Git::Base)

    Underlying git repo



15
16
17
# File 'lib/nabokov/git/git_repo.rb', line 15

def git_repo
  @git_repo
end

#local_pathString

Returns Local path to the repo.

Returns:

  • (String)

    Local path to the repo



13
14
15
# File 'lib/nabokov/git/git_repo.rb', line 13

def local_path
  @local_path
end

#remote_urlString

Returns Repo’s remote URL, could be nil.

Returns:

  • (String)

    Repo’s remote URL, could be nil



11
12
13
# File 'lib/nabokov/git/git_repo.rb', line 11

def remote_url
  @remote_url
end

Instance Method Details

#abort_mergeObject



99
100
101
102
103
# File 'lib/nabokov/git/git_repo.rb', line 99

def abort_merge
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before aborting merge" if @git_repo.nil?
  raise "nothing to abort - git repo doesn't have unfinished merge" unless self.unfinished_merge?
  @git_repo.abort_merge
end

#add(file_path) ⇒ Object



36
37
38
39
40
# File 'lib/nabokov/git/git_repo.rb', line 36

def add(file_path)
  raise "Could not find any file to add at path '#{file_path}'" unless File.exist?(file_path)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before adding new files to the index" if @git_repo.nil?
  @git_repo.add(file_path)
end

#changes?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/nabokov/git/git_repo.rb', line 81

def changes?
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before checking if the git repo has changes" if @git_repo.nil?
  return true if @git_repo.status.deleted.count > 0
  return true if @git_repo.status.added.count > 0
  return true if @git_repo.status.changed.count > 0
  false
end

#checkout_branch(name) ⇒ Object



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

def checkout_branch(name)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before checkouting any branch" if @git_repo.nil?
  raise "branch name could not be nil or zero length" if name.nil? || name.length.zero?
  if @git_repo.is_branch?(name)
    @git_repo.checkout(name)
  else
    @git_repo.checkout(name, { new_branch: true })
  end
end

#cloneObject



26
27
28
29
# File 'lib/nabokov/git/git_repo.rb', line 26

def clone
  raise "Git repo has been already cloned at '#{self.local_path}', please use 'init' instead" if repo_exist_at_local_path
  @git_repo ||= Git.clone(self.remote_url, @local_pathname.basename.to_s, path: @local_pathname.parent.to_s)
end

#commit(message = nil) ⇒ Object



42
43
44
45
46
# File 'lib/nabokov/git/git_repo.rb', line 42

def commit(message = nil)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before commiting new files" if @git_repo.nil?
  message ||= "Automatic commit by nabokov"
  @git_repo.commit(message)
end

#current_branchObject



94
95
96
97
# File 'lib/nabokov/git/git_repo.rb', line 94

def current_branch
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before getting the current branch" if @git_repo.nil?
  return @git_repo.current_branch
end

#delete_branch(name) ⇒ Object



68
69
70
71
72
# File 'lib/nabokov/git/git_repo.rb', line 68

def delete_branch(name)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before deleting any branch" if @git_repo.nil?
  raise "branch name could not be nil or zero length" if name.nil? || name.length.zero?
  @git_repo.branch(name).delete
end

#initObject



31
32
33
34
# File 'lib/nabokov/git/git_repo.rb', line 31

def init
  raise "Git repo has not been cloned yet from '#{self.remote_url}', please use 'clone' instead" unless repo_exist_at_local_path
  @git_repo ||= Git.init(self.local_path)
end

#log(number_of_commits) ⇒ Object



125
126
127
128
129
# File 'lib/nabokov/git/git_repo.rb', line 125

def log(number_of_commits)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before getting the log" if @git_repo.nil?
  commits = @git_repo.log(number_of_commits)
  commits.map(&:sha)
end

#merge_branches(original_branch, branch_to_be_merged) ⇒ Object



74
75
76
77
78
79
# File 'lib/nabokov/git/git_repo.rb', line 74

def merge_branches(original_branch, branch_to_be_merged)
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before merging any branches" if @git_repo.nil?
  raise "original branch name could not be nil or zero length" if original_branch.nil? || original_branch.length.zero?
  raise "branch to be merged in name could not be nil or zero length" if branch_to_be_merged.nil? || branch_to_be_merged.length.zero?
  @git_repo.branch(original_branch).merge(@git_repo.branch(branch_to_be_merged))
end

#pullObject



53
54
55
56
# File 'lib/nabokov/git/git_repo.rb', line 53

def pull
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before pushing any changes to remote" if @git_repo.nil?
  @git_repo.pull
end

#pushObject



48
49
50
51
# File 'lib/nabokov/git/git_repo.rb', line 48

def push
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before pushing any changes to remote" if @git_repo.nil?
  @git_repo.push
end

#reset_to_commit(commit_sha, options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/nabokov/git/git_repo.rb', line 115

def reset_to_commit(commit_sha, options = {})
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before resetting" if @git_repo.nil?
  raise "'commit' is a required parameter and could not be nil" if commit_sha.nil?
  if options[:hard]
    @git_repo.reset_hard(commit_sha)
  else
    @git_repo.reset(commit_sha)
  end
end

#unfinished_merge?Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/nabokov/git/git_repo.rb', line 89

def unfinished_merge?
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before checking if the git repo has unfinished merge" if @git_repo.nil?
  return @git_repo.has_unmerged_files?
end

#unmerged_filesObject



105
106
107
108
109
110
111
112
113
# File 'lib/nabokov/git/git_repo.rb', line 105

def unmerged_files
  raise "'git' is not initialized yet, please call either 'clone' or 'init' before asking for unmerged files" if @git_repo.nil?
  return [] unless @git_repo.has_unmerged_files?
  conflicted_files = []
  @git_repo.each_conflict do |file, your_version, their_version|
    conflicted_files << file
  end
  conflicted_files
end