Class: DTK::Client::GitAdapter
- Inherits:
-
Object
- Object
- DTK::Client::GitAdapter
- Defined in:
- lib/domain/git_adapter.rb
Constant Summary collapse
- TEMP_BRANCH =
"temp_branch"
Instance Attribute Summary collapse
-
#git_repo ⇒ Object
Returns the value of attribute git_repo.
Class Method Summary collapse
Instance Method Summary collapse
- #add_file(file_rel_path, content) ⇒ Object
- #add_remote(name, url) ⇒ Object
- #changed? ⇒ Boolean
- #checkout(branch, opts = {}) ⇒ Object
- #command(*args, &block) ⇒ Object
- #commit(commit_msg = "") ⇒ Object
- #current_branch ⇒ Object
- #delete_branch(branch, opts = {}) ⇒ Object
- #diff_remote_summary(local_branch, remote_reference) ⇒ Object
- #diff_summary(local_branch, remote_reference) ⇒ Object
- #fetch(remote = 'origin') ⇒ Object
- #find_remote_sha(ref) ⇒ Object
- #head_commit_sha ⇒ Object
-
#initialize(repo_dir, local_branch_name = nil) ⇒ GitAdapter
constructor
A new instance of GitAdapter.
- #local_branch_name ⇒ Object
- #local_summary ⇒ Object
- #merge(remote_branch_ref) ⇒ Object
- #merge_relationship(type, ref, opts = {}) ⇒ Object
- #merge_theirs(remote_branch_ref) ⇒ Object
- #new_version ⇒ Object
- #print_status ⇒ Object
- #pull_remote_to_local(remote_branch, local_branch, remote = 'origin') ⇒ Object
- #push(remote_branch_ref, opts = {}) ⇒ Object
- #push_with_remote(remote, remote_branch, opts = {}) ⇒ Object
- #repo_dir ⇒ Object
- #repo_exists? ⇒ Boolean
- #reset_hard(current_branch_sha) ⇒ Object
- #ret_local_branch ⇒ Object
- #rev_list(commit_sha) ⇒ Object
- #rev_list_contains?(container_sha, index_sha) ⇒ Boolean
- #stage_and_commit(commit_msg = "") ⇒ Object
- #stage_changes ⇒ Object
- #staged_commits? ⇒ Boolean
Constructor Details
#initialize(repo_dir, local_branch_name = nil) ⇒ GitAdapter
Returns a new instance of GitAdapter.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/domain/git_adapter.rb', line 29 def initialize(repo_dir, local_branch_name = nil) if DTK::Configuration.get(:debug_grit) logger = Logger.new(STDOUT) logger.level = Logger::INFO @git_repo = Git.init(repo_dir, :log => logger) else @git_repo = Git.init(repo_dir) end # If we want to log GIT interaction # @git_repo = Git.init(repo_dir, :log => Logger.new(STDOUT)) @local_branch_name = local_branch_name end |
Instance Attribute Details
#git_repo ⇒ Object
Returns the value of attribute git_repo.
27 28 29 |
# File 'lib/domain/git_adapter.rb', line 27 def git_repo @git_repo end |
Class Method Details
.clone(repo_url, target_path, branch, opts = {}) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/domain/git_adapter.rb', line 251 def self.clone(repo_url, target_path, branch, opts={}) git_base = handle_git_error{Git.clone(repo_url, target_path)} unless branch.nil? if opts[:track_remote_branch] # This just tracks remote branch begin git_base.checkout(branch) rescue => e # TODO: see if any other kind of error raise DtkError.new("The branch or tag '#{branch}' does not exist on repo '#{repo_url}'") end else # This wil first create a remote branch; # TODO: this might be wrong and should be deprecated git_base.branch(branch).checkout end end git_base end |
Instance Method Details
#add_file(file_rel_path, content) ⇒ Object
228 229 230 231 232 233 |
# File 'lib/domain/git_adapter.rb', line 228 def add_file(file_rel_path, content) content ||= String.new file_path = "#{@git_repo.dir}/#{file_rel_path}" File.open(file_path,"w"){|f|f << content} @git_repo.add(file_path) end |
#add_remote(name, url) ⇒ Object
135 136 137 138 139 |
# File 'lib/domain/git_adapter.rb', line 135 def add_remote(name, url) @git_repo.remove_remote(name) if is_there_remote?(name) @git_repo.add_remote(name, url) end |
#changed? ⇒ Boolean
47 48 49 |
# File 'lib/domain/git_adapter.rb', line 47 def changed? (!(changed().empty? && untracked().empty? && deleted().empty?) || staged_commits?) end |
#checkout(branch, opts = {}) ⇒ Object
214 215 216 |
# File 'lib/domain/git_adapter.rb', line 214 def checkout(branch, opts = {}) @git_repo.checkout(branch, opts) end |
#command(*args, &block) ⇒ Object
43 44 45 |
# File 'lib/domain/git_adapter.rb', line 43 def command(*args, &block) @git_repo.lib.command(*args, &block) end |
#commit(commit_msg = "") ⇒ Object
131 132 133 |
# File 'lib/domain/git_adapter.rb', line 131 def commit(commit_msg = "") @git_repo.commit(commit_msg) end |
#current_branch ⇒ Object
297 298 299 |
# File 'lib/domain/git_adapter.rb', line 297 def current_branch() @git_repo.branches.local.find { |b| b.current } end |
#delete_branch(branch, opts = {}) ⇒ Object
210 211 212 |
# File 'lib/domain/git_adapter.rb', line 210 def delete_branch(branch, opts = {}) @git_repo.branch(branch).delete end |
#diff_remote_summary(local_branch, remote_reference) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/domain/git_adapter.rb', line 97 def diff_remote_summary(local_branch, remote_reference) branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch } branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference } if branch_local_obj && branch_remote_obj difference = @git_repo.lib.diff_full(branch_remote_obj, branch_local_obj) # difference = @git_repo.diff(branch_remote_obj, branch_local_obj) { :diffs => difference } else raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})") end end |
#diff_summary(local_branch, remote_reference) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/domain/git_adapter.rb', line 79 def diff_summary(local_branch, remote_reference) branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch } branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference } if branch_local_obj && branch_remote_obj difference = @git_repo.diff(branch_local_obj, branch_remote_obj) files_modified = difference.stats[:files] ? difference.stats[:files].keys.collect { |file| { :path => file }} : [] { :files_modified => files_modified, :are_there_changes => !files_modified.empty? } else raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})") end end |
#fetch(remote = 'origin') ⇒ Object
141 142 143 |
# File 'lib/domain/git_adapter.rb', line 141 def fetch(remote = 'origin') @git_repo.fetch(remote) end |
#find_remote_sha(ref) ⇒ Object
163 164 165 166 |
# File 'lib/domain/git_adapter.rb', line 163 def find_remote_sha(ref) remote = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == ref} remote.gcommit.sha end |
#head_commit_sha ⇒ Object
159 160 161 |
# File 'lib/domain/git_adapter.rb', line 159 def head_commit_sha() ret_local_branch.gcommit.sha end |
#local_branch_name ⇒ Object
280 281 282 |
# File 'lib/domain/git_adapter.rb', line 280 def local_branch_name ret_local_branch.name end |
#local_summary ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/domain/git_adapter.rb', line 112 def local_summary() { :files_added => (untracked() + added()).collect { |file| { :path => file }}, :files_modified => changed().collect { |file| { :path => file }}, :files_deleted => deleted().collect { |file| { :path => file }}, :are_there_changes => something_changed? } end |
#merge(remote_branch_ref) ⇒ Object
247 248 249 |
# File 'lib/domain/git_adapter.rb', line 247 def merge(remote_branch_ref) @git_repo.merge(remote_branch_ref) end |
#merge_relationship(type, ref, opts = {}) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/domain/git_adapter.rb', line 168 def merge_relationship(type, ref, opts={}) ref_remote, ref_branch = ref.split('/') # fetch remote branch fetch(ref_remote) if opts[:fetch_if_needed] git_reference = case type when :remote_branch @git_repo.branches.remote.find { |r| "#{r.remote}/#{r.name}" == ref } when :local_branch @git_repo.branches.find { |b| b.name == ref } else raise Error.new("Illegal type parameter (#{type}) passed to merge_relationship") end local_sha = ret_local_branch.gcommit.sha opts[:ret_commit_shas][:local_sha] = local_sha if opts[:ret_commit_shas] unless git_reference return :no_remote_ref if type.eql?(:remote_branch) raise Error.new("Cannot find git ref '#{ref}'") end git_reference_sha = git_reference.gcommit.sha opts[:ret_commit_shas][:other_sha] = git_reference_sha if opts[:ret_commit_shas] # shas can be different but content the same if git_reference_sha.eql?(local_sha) || !any_differences?(local_sha, git_reference_sha) :equal else if rev_list_contains?(local_sha, git_reference_sha) :local_ahead elsif rev_list_contains?(git_reference_sha, local_sha) :local_behind else :branchpoint end end end |
#merge_theirs(remote_branch_ref) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/domain/git_adapter.rb', line 303 def merge_theirs(remote_branch_ref) branch = local_branch_name # Git is not agile enoguh to work with following commands so we are using native commands to achive this Dir.chdir(repo_dir) do OsUtil.suspend_output do puts `git checkout -b #{TEMP_BRANCH} #{remote_branch_ref}` puts `git merge #{branch} -s ours` puts `git checkout #{branch}` puts `git reset #{TEMP_BRANCH} --hard` puts `git branch -D #{TEMP_BRANCH}` end end end |
#new_version ⇒ Object
122 123 124 |
# File 'lib/domain/git_adapter.rb', line 122 def new_version() return local_summary() end |
#print_status ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/domain/git_adapter.rb', line 68 def print_status() changes = [changed(), untracked(), deleted()] puts "\nModified files:\n".colorize(:green) unless changes[0].empty? changes[0].each { |item| puts "\t#{item}" } puts "\nAdded files:\n".colorize(:yellow) unless changes[1].empty? changes[1].each { |item| puts "\t#{item}" } puts "\nDeleted files:\n".colorize(:red) unless changes[2].empty? changes[2].each { |item| puts "\t#{item}" } puts "" end |
#pull_remote_to_local(remote_branch, local_branch, remote = 'origin') ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/domain/git_adapter.rb', line 235 def pull_remote_to_local(remote_branch, local_branch, remote='origin') # special case; if no branches and local_branch differs from master # creates master plus local_branch # special_case must be calculated before pull special_case = current_branch().nil? and local_branch != 'master' @git_repo.pull(remote,"#{remote_branch}:#{local_branch}") if special_case @git_repo.branch(local_branch).checkout @git_repo.branch('master').delete end end |
#push(remote_branch_ref, opts = {}) ⇒ Object
218 219 220 221 |
# File 'lib/domain/git_adapter.rb', line 218 def push(remote_branch_ref, opts={}) remote, remote_branch = remote_branch_ref.split('/') push_with_remote(remote, remote_branch, opts) end |
#push_with_remote(remote, remote_branch, opts = {}) ⇒ Object
223 224 225 226 |
# File 'lib/domain/git_adapter.rb', line 223 def push_with_remote(remote, remote_branch, opts={}) branch_for_push = "#{local_branch_name}:refs/heads/#{remote_branch||local_branch_name}" @git_repo.push(remote, branch_for_push, opts) end |
#repo_dir ⇒ Object
272 273 274 |
# File 'lib/domain/git_adapter.rb', line 272 def repo_dir @git_repo.dir.path end |
#repo_exists? ⇒ Boolean
276 277 278 |
# File 'lib/domain/git_adapter.rb', line 276 def repo_exists? File.exists?(repo_dir) end |
#reset_hard(current_branch_sha) ⇒ Object
318 319 320 |
# File 'lib/domain/git_adapter.rb', line 318 def reset_hard(current_branch_sha) @git_repo.reset_hard(current_branch_sha) end |
#ret_local_branch ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/domain/git_adapter.rb', line 284 def ret_local_branch # This build in assumption that just one local branch unless ret = current_branch() raise Error.new("Unexpected that current_branch() is nil") end if @local_branch_name unless ret.name == @local_branch_name raise Error.new("Unexpected that @local_branch_name (#{@local_branch_name}) does not equal current branch (#{current_branch()})") end end ret end |
#rev_list(commit_sha) ⇒ Object
145 146 147 |
# File 'lib/domain/git_adapter.rb', line 145 def rev_list(commit_sha) git_command('rev-list', commit_sha) end |
#rev_list_contains?(container_sha, index_sha) ⇒ Boolean
154 155 156 157 |
# File 'lib/domain/git_adapter.rb', line 154 def rev_list_contains?(container_sha, index_sha) results = rev_list(container_sha) !results.split("\n").grep(index_sha).empty? end |
#stage_and_commit(commit_msg = "") ⇒ Object
126 127 128 129 |
# File 'lib/domain/git_adapter.rb', line 126 def stage_and_commit(commit_msg = "") stage_changes() commit(commit_msg) end |
#stage_changes ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/domain/git_adapter.rb', line 51 def stage_changes() handle_git_error do @git_repo.add(untracked()) @git_repo.add(added()) @git_repo.add(changed()) end deleted().each do |file| begin @git_repo.remove(file) rescue # ignore this error means file has already been staged # we cannot support status of file, in 1.8.7 so this is # solution for that end end end |
#staged_commits? ⇒ Boolean
149 150 151 152 |
# File 'lib/domain/git_adapter.rb', line 149 def staged_commits?() response = git_command('diff','--cached') !response.empty? end |