dtk-client 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +13 -5
  2. data/dtk-client.gemspec +2 -2
  3. data/lib/auxiliary.rb +1 -1
  4. data/lib/client.rb +1 -1
  5. data/lib/command_helper.rb +1 -1
  6. data/lib/command_helpers/git_repo/merge.rb +1 -1
  7. data/lib/command_helpers/git_repo.rb +1 -1
  8. data/lib/command_helpers/jenkins_client/config_xml.rb +1 -1
  9. data/lib/command_helpers/jenkins_client.rb +1 -1
  10. data/lib/command_helpers/service_importer.rb +6 -2
  11. data/lib/command_helpers/service_link.rb +1 -1
  12. data/lib/command_helpers/test_module_creator.rb +1 -1
  13. data/lib/commands/common/thor/access_control.rb +1 -1
  14. data/lib/commands/common/thor/action_result_handler.rb +57 -57
  15. data/lib/commands/common/thor/assembly_template.rb +1 -1
  16. data/lib/commands/common/thor/assembly_workspace.rb +15 -5
  17. data/lib/commands/common/thor/base_command_helper.rb +1 -1
  18. data/lib/commands/common/thor/clone.rb +1 -1
  19. data/lib/commands/common/thor/common.rb +1 -1
  20. data/lib/commands/common/thor/common_base.rb +1 -1
  21. data/lib/commands/common/thor/create_target.rb +1 -1
  22. data/lib/commands/common/thor/edit.rb +1 -1
  23. data/lib/commands/common/thor/inventory_parser.rb +1 -1
  24. data/lib/commands/common/thor/list_diffs.rb +111 -111
  25. data/lib/commands/common/thor/module/import.rb +1 -1
  26. data/lib/commands/common/thor/module.rb +18 -5
  27. data/lib/commands/common/thor/node.rb +1 -1
  28. data/lib/commands/common/thor/poller.rb +1 -1
  29. data/lib/commands/common/thor/pull_clone_changes.rb +1 -1
  30. data/lib/commands/common/thor/pull_from_remote.rb +1 -1
  31. data/lib/commands/common/thor/puppet_forge.rb +1 -1
  32. data/lib/commands/common/thor/purge_clone.rb +1 -1
  33. data/lib/commands/common/thor/push_clone_changes.rb +1 -1
  34. data/lib/commands/common/thor/push_to_remote.rb +1 -1
  35. data/lib/commands/common/thor/remotes.rb +1 -1
  36. data/lib/commands/common/thor/reparse.rb +1 -1
  37. data/lib/commands/common/thor/set_required_attributes.rb +1 -1
  38. data/lib/commands/thor/account.rb +1 -1
  39. data/lib/commands/thor/assembly.rb +1 -1
  40. data/lib/commands/thor/attribute.rb +1 -1
  41. data/lib/commands/thor/component.rb +1 -1
  42. data/lib/commands/thor/component_module.rb +1 -1
  43. data/lib/commands/thor/component_template.rb +1 -1
  44. data/lib/commands/thor/dependency.rb +1 -1
  45. data/lib/commands/thor/developer.rb +127 -127
  46. data/lib/commands/thor/dtk.rb +1 -1
  47. data/lib/commands/thor/library.rb +1 -1
  48. data/lib/commands/thor/node.rb +1 -1
  49. data/lib/commands/thor/node_group.rb +1 -1
  50. data/lib/commands/thor/node_template.rb +1 -1
  51. data/lib/commands/thor/project.rb +17 -17
  52. data/lib/commands/thor/provider.rb +7 -5
  53. data/lib/commands/thor/remotes.rb +1 -1
  54. data/lib/commands/thor/service.rb +18 -3
  55. data/lib/commands/thor/service_module.rb +1 -1
  56. data/lib/commands/thor/target.rb +233 -231
  57. data/lib/commands/thor/task.rb +1 -1
  58. data/lib/commands/thor/test_module.rb +1 -1
  59. data/lib/commands/thor/workspace.rb +22 -3
  60. data/lib/commands.rb +40 -40
  61. data/lib/config/configuration.rb +1 -1
  62. data/lib/config/disk_cacher.rb +1 -1
  63. data/lib/configurator.rb +1 -1
  64. data/lib/context_router.rb +1 -1
  65. data/lib/core.rb +1 -1
  66. data/lib/domain/git_adapter.rb +395 -395
  67. data/lib/domain/git_error_handler.rb +47 -47
  68. data/lib/domain/response/error_handler.rb +13 -4
  69. data/lib/domain/response.rb +1 -1
  70. data/lib/dtk-client/version.rb +3 -3
  71. data/lib/dtk_client.rb +1 -1
  72. data/lib/dtk_constants.rb +1 -1
  73. data/lib/dtk_error.rb +1 -1
  74. data/lib/dtk_logger.rb +96 -96
  75. data/lib/error.rb +1 -1
  76. data/lib/execute/cli_pure/cli_rerouter.rb +1 -1
  77. data/lib/execute/command/api_call/map.rb +1 -1
  78. data/lib/execute/command/api_call/service.rb +1 -1
  79. data/lib/execute/command/api_call/translation_term.rb +1 -1
  80. data/lib/execute/command/api_call.rb +1 -1
  81. data/lib/execute/command/rest_call.rb +1 -1
  82. data/lib/execute/command.rb +1 -1
  83. data/lib/execute/command_processor/rest_call.rb +1 -1
  84. data/lib/execute/command_processor.rb +1 -1
  85. data/lib/execute/execute_context/result_store.rb +1 -1
  86. data/lib/execute/execute_context.rb +1 -1
  87. data/lib/execute/script/add_tenant.rb +1 -1
  88. data/lib/execute/script.rb +1 -1
  89. data/lib/execute.rb +1 -1
  90. data/lib/parser/adapters/option_parser.rb +1 -1
  91. data/lib/parser/adapters/thor/common_option_defs.rb +1 -1
  92. data/lib/parser/adapters/thor.rb +1 -1
  93. data/lib/require_first.rb +87 -87
  94. data/lib/search_hash.rb +1 -1
  95. data/lib/shell/context.rb +1 -1
  96. data/lib/shell/context_aux.rb +1 -1
  97. data/lib/shell/domain/active_context.rb +1 -1
  98. data/lib/shell/domain/context_entity.rb +1 -1
  99. data/lib/shell/domain/context_params.rb +1 -1
  100. data/lib/shell/domain/override_tasks.rb +1 -1
  101. data/lib/shell/domain/shadow_entity.rb +1 -1
  102. data/lib/shell/header_shell.rb +1 -1
  103. data/lib/shell/help_monkey_patch.rb +1 -1
  104. data/lib/shell/interactive_wizard.rb +1 -1
  105. data/lib/shell/message_queue.rb +1 -1
  106. data/lib/shell/parse_monkey_patch.rb +1 -1
  107. data/lib/shell/status_monitor.rb +107 -107
  108. data/lib/shell.rb +1 -1
  109. data/lib/task_status/refresh_mode.rb +1 -1
  110. data/lib/task_status/snapshot_mode.rb +1 -1
  111. data/lib/task_status/stream_mode/element/format.rb +1 -1
  112. data/lib/task_status/stream_mode/element/hierarchical_task/result/action.rb +1 -1
  113. data/lib/task_status/stream_mode/element/hierarchical_task/result.rb +1 -1
  114. data/lib/task_status/stream_mode/element/hierarchical_task/steps/action.rb +1 -1
  115. data/lib/task_status/stream_mode/element/hierarchical_task/steps/components.rb +1 -1
  116. data/lib/task_status/stream_mode/element/hierarchical_task/steps/node_level.rb +1 -1
  117. data/lib/task_status/stream_mode/element/hierarchical_task/steps.rb +1 -1
  118. data/lib/task_status/stream_mode/element/hierarchical_task.rb +1 -1
  119. data/lib/task_status/stream_mode/element/render.rb +1 -1
  120. data/lib/task_status/stream_mode/element/stage/render.rb +1 -1
  121. data/lib/task_status/stream_mode/element/stage.rb +1 -1
  122. data/lib/task_status/stream_mode/element/task_end.rb +1 -1
  123. data/lib/task_status/stream_mode/element/task_start.rb +1 -1
  124. data/lib/task_status/stream_mode/element.rb +1 -1
  125. data/lib/task_status/stream_mode.rb +1 -1
  126. data/lib/task_status.rb +1 -1
  127. data/lib/util/common_util.rb +1 -1
  128. data/lib/util/console.rb +1 -1
  129. data/lib/util/dtk_puppet.rb +1 -1
  130. data/lib/util/module_util.rb +1 -1
  131. data/lib/util/os_util.rb +6 -2
  132. data/lib/util/permission_util.rb +1 -1
  133. data/lib/util/remote_dependency_util.rb +1 -1
  134. data/lib/util/ssh_util.rb +77 -77
  135. data/lib/view_processor/augmented_simple_list.rb +1 -1
  136. data/lib/view_processor/hash_pretty_print.rb +1 -1
  137. data/lib/view_processor/simple_list.rb +1 -1
  138. data/lib/view_processor/table_print.rb +1 -1
  139. data/lib/view_processor.rb +1 -1
  140. data/spec/lib/spec_helper.rb +1 -1
  141. data/spec/lib/spec_thor.rb +1 -1
  142. data/spec/node_template_spec.rb +1 -1
  143. data/spec/table_print_spec.rb +1 -1
  144. data/spec/target_spec.rb +1 -1
  145. data/spec/task_spec.rb +1 -1
  146. metadata +20 -20
@@ -15,398 +15,398 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
- require 'git'
19
- # monkey patch
20
- class Git::Lib
21
- public :command
22
- end
23
-
24
- module DTK
25
- module Client
26
- class GitAdapter
27
- attr_accessor :git_repo
28
-
29
- def initialize(repo_dir, local_branch_name = nil)
30
-
31
- if DTK::Configuration.get(:debug_grit)
32
- logger = Logger.new(STDOUT)
33
- logger.level = Logger::INFO
34
- @git_repo = Git.init(repo_dir, :log => logger)
35
- else
36
- @git_repo = Git.init(repo_dir)
37
- end
38
- # If we want to log GIT interaction
39
- # @git_repo = Git.init(repo_dir, :log => Logger.new(STDOUT))
40
- @local_branch_name = local_branch_name
41
- end
42
-
43
- def command(*args, &block)
44
- @git_repo.lib.command(*args, &block)
45
- end
46
-
47
- def changed?
48
- (!(changed().empty? && untracked().empty? && deleted().empty?) || staged_commits?)
49
- end
50
-
51
- def stage_changes()
52
- handle_git_error do
53
- @git_repo.add(untracked())
54
- @git_repo.add(added())
55
- @git_repo.add(changed())
56
- end
57
- deleted().each do |file|
58
- begin
59
- @git_repo.remove(file)
60
- rescue
61
- # ignore this error means file has already been staged
62
- # we cannot support status of file, in 1.8.7 so this is
63
- # solution for that
64
- end
65
- end
66
- end
67
-
68
- def print_status()
69
- changes = [changed(), untracked(), deleted()]
70
- puts "\nModified files:\n".colorize(:green) unless changes[0].empty?
71
- changes[0].each { |item| puts "\t#{item}" }
72
- puts "\nAdded files:\n".colorize(:yellow) unless changes[1].empty?
73
- changes[1].each { |item| puts "\t#{item}" }
74
- puts "\nDeleted files:\n".colorize(:red) unless changes[2].empty?
75
- changes[2].each { |item| puts "\t#{item}" }
76
- puts ""
77
- end
78
-
79
- def diff_summary(local_branch, remote_reference)
80
- branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch }
81
- branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference }
82
-
83
- if branch_local_obj && branch_remote_obj
84
-
85
- difference = @git_repo.diff(branch_local_obj, branch_remote_obj)
86
-
87
- files_modified = difference.stats[:files] ? difference.stats[:files].keys.collect { |file| { :path => file }} : []
88
- {
89
- :files_modified => files_modified,
90
- :are_there_changes => !files_modified.empty?
91
- }
92
- else
93
- raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})")
94
- end
95
- end
96
-
97
- def diff_remote_summary(local_branch, remote_reference)
98
- branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch }
99
- branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference }
100
-
101
- if branch_local_obj && branch_remote_obj
102
- difference = @git_repo.lib.diff_full(branch_remote_obj, branch_local_obj)
103
- # difference = @git_repo.diff(branch_remote_obj, branch_local_obj)
104
- {
105
- :diffs => difference
106
- }
107
- else
108
- raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})")
109
- end
110
- end
111
-
112
- def local_summary()
113
-
114
- {
115
- :files_added => (untracked() + added()).collect { |file| { :path => file }},
116
- :files_modified => changed().collect { |file| { :path => file }},
117
- :files_deleted => deleted().collect { |file| { :path => file }},
118
- :are_there_changes => something_changed?
119
- }
120
- end
121
-
122
- def new_version()
123
- return local_summary()
124
- end
125
-
126
- def stage_and_commit(commit_msg = "")
127
- stage_changes()
128
- commit(commit_msg)
129
- end
130
-
131
- def commit(commit_msg = "")
132
- @git_repo.commit(commit_msg)
133
- end
134
-
135
- def add_remote(name, url)
136
- @git_repo.remove_remote(name) if is_there_remote?(name)
137
-
138
- @git_repo.add_remote(name, url)
139
- end
140
-
141
- def fetch(remote = 'origin')
142
- @git_repo.fetch(remote)
143
- end
144
-
145
- def rev_list(commit_sha)
146
- git_command('rev-list', commit_sha)
147
- end
148
-
149
- def staged_commits?()
150
- response = git_command('diff','--cached')
151
- !response.empty?
152
- end
153
-
154
- def rev_list_contains?(container_sha, index_sha)
155
- results = rev_list(container_sha)
156
- !results.split("\n").grep(index_sha).empty?
157
- end
158
-
159
- def head_commit_sha()
160
- ret_local_branch.gcommit.sha
161
- end
162
-
163
- def find_remote_sha(ref)
164
- remote = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == ref}
165
- remote.gcommit.sha
166
- end
167
-
168
- def merge_relationship(type, ref, opts={})
169
- ref_remote, ref_branch = ref.split('/')
170
- # fetch remote branch
171
- fetch(ref_remote) if opts[:fetch_if_needed]
172
-
173
-
174
- git_reference = case type
175
- when :remote_branch
176
- @git_repo.branches.remote.find { |r| "#{r.remote}/#{r.name}" == ref }
177
- when :local_branch
178
- @git_repo.branches.find { |b| b.name == ref }
179
- else
180
- raise Error.new("Illegal type parameter (#{type}) passed to merge_relationship")
181
- end
182
-
183
- local_sha = ret_local_branch.gcommit.sha
184
-
185
- opts[:ret_commit_shas][:local_sha] = local_sha if opts[:ret_commit_shas]
186
-
187
- unless git_reference
188
- return :no_remote_ref if type.eql?(:remote_branch)
189
-
190
- raise Error.new("Cannot find git ref '#{ref}'")
191
- end
192
-
193
- git_reference_sha = git_reference.gcommit.sha
194
- opts[:ret_commit_shas][:other_sha] = git_reference_sha if opts[:ret_commit_shas]
195
-
196
- # shas can be different but content the same
197
- if git_reference_sha.eql?(local_sha) || !any_differences?(local_sha, git_reference_sha)
198
- :equal
199
- else
200
- if rev_list_contains?(local_sha, git_reference_sha)
201
- :local_ahead
202
- elsif rev_list_contains?(git_reference_sha, local_sha)
203
- :local_behind
204
- else
205
- :branchpoint
206
- end
207
- end
208
- end
209
-
210
- def delete_branch(branch, opts = {})
211
- @git_repo.branch(branch).delete
212
- end
213
-
214
- def checkout(branch, opts = {})
215
- @git_repo.checkout(branch, opts)
216
- end
217
-
218
- def push(remote_branch_ref, opts={})
219
- remote, remote_branch = remote_branch_ref.split('/')
220
- push_with_remote(remote, remote_branch, opts)
221
- end
222
-
223
- def push_with_remote(remote, remote_branch, opts={})
224
- branch_for_push = "#{local_branch_name}:refs/heads/#{remote_branch||local_branch_name}"
225
- @git_repo.push(remote, branch_for_push, opts)
226
- end
227
-
228
- def add_file(file_rel_path, content)
229
- content ||= String.new
230
- file_path = "#{@git_repo.dir}/#{file_rel_path}"
231
- File.open(file_path,"w"){|f|f << content}
232
- @git_repo.add(file_path)
233
- end
234
-
235
- def pull_remote_to_local(remote_branch, local_branch, remote='origin')
236
- # special case; if no branches and local_branch differs from master
237
- # creates master plus local_branch
238
- # special_case must be calculated before pull
239
- special_case = current_branch().nil? and local_branch != 'master'
240
- @git_repo.pull(remote,"#{remote_branch}:#{local_branch}")
241
- if special_case
242
- @git_repo.branch(local_branch).checkout
243
- @git_repo.branch('master').delete
244
- end
245
- end
246
-
247
- def merge(remote_branch_ref)
248
- @git_repo.merge(remote_branch_ref)
249
- end
250
-
251
- def self.clone(repo_url, target_path, branch, opts={})
252
- git_base = handle_git_error{Git.clone(repo_url, target_path)}
253
-
254
- unless branch.nil?
255
- if opts[:track_remote_branch]
256
- # This just tracks remote branch
257
- begin
258
- git_base.checkout(branch)
259
- rescue => e
260
- # TODO: see if any other kind of error
261
- raise DtkError.new("The branch or tag '#{branch}' does not exist on repo '#{repo_url}'")
262
- end
263
- else
264
- # This wil first create a remote branch;
265
- # TODO: this might be wrong and should be deprecated
266
- git_base.branch(branch).checkout
267
- end
268
- end
269
- git_base
270
- end
271
-
272
- def repo_dir
273
- @git_repo.dir.path
274
- end
275
-
276
- def repo_exists?
277
- File.exists?(repo_dir)
278
- end
279
-
280
- def local_branch_name
281
- ret_local_branch.name
282
- end
283
-
284
- def ret_local_branch
285
- # This build in assumption that just one local branch
286
- unless ret = current_branch()
287
- raise Error.new("Unexpected that current_branch() is nil")
288
- end
289
- if @local_branch_name
290
- unless ret.name == @local_branch_name
291
- raise Error.new("Unexpected that @local_branch_name (#{@local_branch_name}) does not equal current branch (#{current_branch()})")
292
- end
293
- end
294
- ret
295
- end
296
-
297
- def current_branch()
298
- @git_repo.branches.local.find { |b| b.current }
299
- end
300
-
301
- TEMP_BRANCH = "temp_branch"
302
-
303
- def merge_theirs(remote_branch_ref)
304
- branch = local_branch_name
305
-
306
- # Git is not agile enoguh to work with following commands so we are using native commands to achive this
307
- Dir.chdir(repo_dir) do
308
- OsUtil.suspend_output do
309
- puts `git checkout -b #{TEMP_BRANCH} #{remote_branch_ref}`
310
- puts `git merge #{branch} -s ours`
311
- puts `git checkout #{branch}`
312
- puts `git reset #{TEMP_BRANCH} --hard`
313
- puts `git branch -D #{TEMP_BRANCH}`
314
- end
315
- end
316
- end
317
-
318
- def reset_hard(current_branch_sha)
319
- @git_repo.reset_hard(current_branch_sha)
320
- end
321
-
322
- private
323
- def handle_git_error(&block)
324
- self.class.handle_git_error(&block)
325
- end
326
- def self.handle_git_error(&block)
327
- ret = nil
328
- begin
329
- ret = yield
330
- rescue => e
331
- unless e.respond_to?(:message)
332
- raise e
333
- else
334
- err_msg = e.message
335
- lines = err_msg.split("\n")
336
- if lines.last =~ GitErrorPattern
337
- err_msg = error_msg_when_git_error(lines)
338
- end
339
- raise DtkError.new(err_msg)
340
- end
341
- end
342
- ret
343
- end
344
- GitErrorPattern = /^fatal:/
345
- def self.error_msg_when_git_error(lines)
346
- ret = lines.last.gsub(GitErrorPattern,'').strip()
347
- # TODO start putting in special cases here
348
- if ret =~ /adding files failed/
349
- if lines.first =~ /\.git/
350
- ret = "Cannot add files that are in a .git directory; remove any nested .git directory"
351
- end
352
- end
353
- ret
354
- end
355
-
356
- # Method bellow show different behavior when working with 1.8.7
357
- # so based on Hash response we know it it is:
358
- # Hash => 1.9.3 +
359
- # Array => 1.8.7
360
- #
361
-
362
- def changed
363
- status.is_a?(Hash) ? status.changed().keys : status.changed().collect { |file| file.first }
364
- end
365
-
366
- def untracked
367
- status.is_a?(Hash) ? status.untracked().keys : status.untracked().collect { |file| file.first }
368
- end
369
-
370
- def deleted
371
- status.is_a?(Hash) ? status.deleted().keys : status.deleted().collect { |file| file.first }
372
- end
373
-
374
- def added
375
- status.is_a?(Hash) ? status.added().keys : status.added().collect { |file| file.first }
376
- end
377
-
378
- def something_changed?
379
- ![changed, untracked, deleted, added].flatten.empty?
380
- end
381
-
382
- def status
383
- @git_repo.status
384
- end
385
-
386
- def is_there_remote?(remote_name)
387
- @git_repo.remotes.find { |r| r.name == remote_name }
388
- end
389
-
390
- def any_differences?(sha1, sha2)
391
- @git_repo.diff(sha1, sha2).size > 0
392
- end
393
-
394
- def git_command(cmd, opts=[])
395
- ENV['GIT_DIR'] = "#{@git_repo.dir.path}/.git"
396
- ENV['GIT_INDEX_FILE'] = @git_repo.index.path
397
-
398
- path = @git_repo.dir.path
399
-
400
- opts = [opts].flatten.join(' ')
401
-
402
- response = `git #{cmd} #{opts}`.chomp
403
-
404
- ENV.delete('GIT_DIR')
405
- ENV.delete('GIT_INDEX_FILE')
406
-
407
- return response
408
- end
409
-
410
- end
411
- end
412
- end
18
+ require 'git'
19
+ # monkey patch
20
+ class Git::Lib
21
+ public :command
22
+ end
23
+
24
+ module DTK
25
+ module Client
26
+ class GitAdapter
27
+ attr_accessor :git_repo
28
+
29
+ def initialize(repo_dir, local_branch_name = nil)
30
+
31
+ if DTK::Configuration.get(:debug_grit)
32
+ logger = Logger.new(STDOUT)
33
+ logger.level = Logger::INFO
34
+ @git_repo = Git.init(repo_dir, :log => logger)
35
+ else
36
+ @git_repo = Git.init(repo_dir)
37
+ end
38
+ # If we want to log GIT interaction
39
+ # @git_repo = Git.init(repo_dir, :log => Logger.new(STDOUT))
40
+ @local_branch_name = local_branch_name
41
+ end
42
+
43
+ def command(*args, &block)
44
+ @git_repo.lib.command(*args, &block)
45
+ end
46
+
47
+ def changed?
48
+ (!(changed().empty? && untracked().empty? && deleted().empty?) || staged_commits?)
49
+ end
50
+
51
+ def stage_changes()
52
+ handle_git_error do
53
+ @git_repo.add(untracked())
54
+ @git_repo.add(added())
55
+ @git_repo.add(changed())
56
+ end
57
+ deleted().each do |file|
58
+ begin
59
+ @git_repo.remove(file)
60
+ rescue
61
+ # ignore this error means file has already been staged
62
+ # we cannot support status of file, in 1.8.7 so this is
63
+ # solution for that
64
+ end
65
+ end
66
+ end
67
+
68
+ def print_status()
69
+ changes = [changed(), untracked(), deleted()]
70
+ puts "\nModified files:\n".colorize(:green) unless changes[0].empty?
71
+ changes[0].each { |item| puts "\t#{item}" }
72
+ puts "\nAdded files:\n".colorize(:yellow) unless changes[1].empty?
73
+ changes[1].each { |item| puts "\t#{item}" }
74
+ puts "\nDeleted files:\n".colorize(:red) unless changes[2].empty?
75
+ changes[2].each { |item| puts "\t#{item}" }
76
+ puts ""
77
+ end
78
+
79
+ def diff_summary(local_branch, remote_reference)
80
+ branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch }
81
+ branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference }
82
+
83
+ if branch_local_obj && branch_remote_obj
84
+
85
+ difference = @git_repo.diff(branch_local_obj, branch_remote_obj)
86
+
87
+ files_modified = difference.stats[:files] ? difference.stats[:files].keys.collect { |file| { :path => file }} : []
88
+ {
89
+ :files_modified => files_modified,
90
+ :are_there_changes => !files_modified.empty?
91
+ }
92
+ else
93
+ raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})")
94
+ end
95
+ end
96
+
97
+ def diff_remote_summary(local_branch, remote_reference)
98
+ branch_local_obj = @git_repo.branches.local.find { |b| b.name == local_branch }
99
+ branch_remote_obj = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == remote_reference }
100
+
101
+ if branch_local_obj && branch_remote_obj
102
+ difference = @git_repo.lib.diff_full(branch_remote_obj, branch_local_obj)
103
+ # difference = @git_repo.diff(branch_remote_obj, branch_local_obj)
104
+ {
105
+ :diffs => difference
106
+ }
107
+ else
108
+ raise Error.new("Error finding branches: local branch '#{local_branch}' (found: #{!branch_local_obj.nil?}), remote branch '#{remote_reference}' (found: #{!branch_remote_obj.nil?})")
109
+ end
110
+ end
111
+
112
+ def local_summary()
113
+
114
+ {
115
+ :files_added => (untracked() + added()).collect { |file| { :path => file }},
116
+ :files_modified => changed().collect { |file| { :path => file }},
117
+ :files_deleted => deleted().collect { |file| { :path => file }},
118
+ :are_there_changes => something_changed?
119
+ }
120
+ end
121
+
122
+ def new_version()
123
+ return local_summary()
124
+ end
125
+
126
+ def stage_and_commit(commit_msg = "")
127
+ stage_changes()
128
+ commit(commit_msg)
129
+ end
130
+
131
+ def commit(commit_msg = "")
132
+ @git_repo.commit(commit_msg)
133
+ end
134
+
135
+ def add_remote(name, url)
136
+ @git_repo.remove_remote(name) if is_there_remote?(name)
137
+
138
+ @git_repo.add_remote(name, url)
139
+ end
140
+
141
+ def fetch(remote = 'origin')
142
+ @git_repo.fetch(remote)
143
+ end
144
+
145
+ def rev_list(commit_sha)
146
+ git_command('rev-list', commit_sha)
147
+ end
148
+
149
+ def staged_commits?()
150
+ response = git_command('diff','--cached')
151
+ !response.empty?
152
+ end
153
+
154
+ def rev_list_contains?(container_sha, index_sha)
155
+ results = rev_list(container_sha)
156
+ !results.split("\n").grep(index_sha).empty?
157
+ end
158
+
159
+ def head_commit_sha()
160
+ ret_local_branch.gcommit.sha
161
+ end
162
+
163
+ def find_remote_sha(ref)
164
+ remote = @git_repo.branches.remote.find{|r| "#{r.remote}/#{r.name}" == ref}
165
+ remote.gcommit.sha
166
+ end
167
+
168
+ def merge_relationship(type, ref, opts={})
169
+ ref_remote, ref_branch = ref.split('/')
170
+ # fetch remote branch
171
+ fetch(ref_remote) if opts[:fetch_if_needed]
172
+
173
+
174
+ git_reference = case type
175
+ when :remote_branch
176
+ @git_repo.branches.remote.find { |r| "#{r.remote}/#{r.name}" == ref }
177
+ when :local_branch
178
+ @git_repo.branches.find { |b| b.name == ref }
179
+ else
180
+ raise Error.new("Illegal type parameter (#{type}) passed to merge_relationship")
181
+ end
182
+
183
+ local_sha = ret_local_branch.gcommit.sha
184
+
185
+ opts[:ret_commit_shas][:local_sha] = local_sha if opts[:ret_commit_shas]
186
+
187
+ unless git_reference
188
+ return :no_remote_ref if type.eql?(:remote_branch)
189
+
190
+ raise Error.new("Cannot find git ref '#{ref}'")
191
+ end
192
+
193
+ git_reference_sha = git_reference.gcommit.sha
194
+ opts[:ret_commit_shas][:other_sha] = git_reference_sha if opts[:ret_commit_shas]
195
+
196
+ # shas can be different but content the same
197
+ if git_reference_sha.eql?(local_sha) || !any_differences?(local_sha, git_reference_sha)
198
+ :equal
199
+ else
200
+ if rev_list_contains?(local_sha, git_reference_sha)
201
+ :local_ahead
202
+ elsif rev_list_contains?(git_reference_sha, local_sha)
203
+ :local_behind
204
+ else
205
+ :branchpoint
206
+ end
207
+ end
208
+ end
209
+
210
+ def delete_branch(branch, opts = {})
211
+ @git_repo.branch(branch).delete
212
+ end
213
+
214
+ def checkout(branch, opts = {})
215
+ @git_repo.checkout(branch, opts)
216
+ end
217
+
218
+ def push(remote_branch_ref, opts={})
219
+ remote, remote_branch = remote_branch_ref.split('/')
220
+ push_with_remote(remote, remote_branch, opts)
221
+ end
222
+
223
+ def push_with_remote(remote, remote_branch, opts={})
224
+ branch_for_push = "#{local_branch_name}:refs/heads/#{remote_branch||local_branch_name}"
225
+ @git_repo.push(remote, branch_for_push, opts)
226
+ end
227
+
228
+ def add_file(file_rel_path, content)
229
+ content ||= String.new
230
+ file_path = "#{@git_repo.dir}/#{file_rel_path}"
231
+ File.open(file_path,"w"){|f|f << content}
232
+ @git_repo.add(file_path)
233
+ end
234
+
235
+ def pull_remote_to_local(remote_branch, local_branch, remote='origin')
236
+ # special case; if no branches and local_branch differs from master
237
+ # creates master plus local_branch
238
+ # special_case must be calculated before pull
239
+ special_case = current_branch().nil? and local_branch != 'master'
240
+ @git_repo.pull(remote,"#{remote_branch}:#{local_branch}")
241
+ if special_case
242
+ @git_repo.branch(local_branch).checkout
243
+ @git_repo.branch('master').delete
244
+ end
245
+ end
246
+
247
+ def merge(remote_branch_ref)
248
+ @git_repo.merge(remote_branch_ref)
249
+ end
250
+
251
+ def self.clone(repo_url, target_path, branch, opts={})
252
+ git_base = handle_git_error{Git.clone(repo_url, target_path)}
253
+
254
+ unless branch.nil?
255
+ if opts[:track_remote_branch]
256
+ # This just tracks remote branch
257
+ begin
258
+ git_base.checkout(branch)
259
+ rescue => e
260
+ # TODO: see if any other kind of error
261
+ raise DtkError.new("The branch or tag '#{branch}' does not exist on repo '#{repo_url}'")
262
+ end
263
+ else
264
+ # This wil first create a remote branch;
265
+ # TODO: this might be wrong and should be deprecated
266
+ git_base.branch(branch).checkout
267
+ end
268
+ end
269
+ git_base
270
+ end
271
+
272
+ def repo_dir
273
+ @git_repo.dir.path
274
+ end
275
+
276
+ def repo_exists?
277
+ File.exists?(repo_dir)
278
+ end
279
+
280
+ def local_branch_name
281
+ ret_local_branch.name
282
+ end
283
+
284
+ def ret_local_branch
285
+ # This build in assumption that just one local branch
286
+ unless ret = current_branch()
287
+ raise Error.new("Unexpected that current_branch() is nil")
288
+ end
289
+ if @local_branch_name
290
+ unless ret.name == @local_branch_name
291
+ raise Error.new("Unexpected that @local_branch_name (#{@local_branch_name}) does not equal current branch (#{current_branch()})")
292
+ end
293
+ end
294
+ ret
295
+ end
296
+
297
+ def current_branch()
298
+ @git_repo.branches.local.find { |b| b.current }
299
+ end
300
+
301
+ TEMP_BRANCH = "temp_branch"
302
+
303
+ def merge_theirs(remote_branch_ref)
304
+ branch = local_branch_name
305
+
306
+ # Git is not agile enoguh to work with following commands so we are using native commands to achive this
307
+ Dir.chdir(repo_dir) do
308
+ OsUtil.suspend_output do
309
+ puts `git checkout -b #{TEMP_BRANCH} #{remote_branch_ref}`
310
+ puts `git merge #{branch} -s ours`
311
+ puts `git checkout #{branch}`
312
+ puts `git reset #{TEMP_BRANCH} --hard`
313
+ puts `git branch -D #{TEMP_BRANCH}`
314
+ end
315
+ end
316
+ end
317
+
318
+ def reset_hard(current_branch_sha)
319
+ @git_repo.reset_hard(current_branch_sha)
320
+ end
321
+
322
+ private
323
+ def handle_git_error(&block)
324
+ self.class.handle_git_error(&block)
325
+ end
326
+ def self.handle_git_error(&block)
327
+ ret = nil
328
+ begin
329
+ ret = yield
330
+ rescue => e
331
+ unless e.respond_to?(:message)
332
+ raise e
333
+ else
334
+ err_msg = e.message
335
+ lines = err_msg.split("\n")
336
+ if lines.last =~ GitErrorPattern
337
+ err_msg = error_msg_when_git_error(lines)
338
+ end
339
+ raise DtkError.new(err_msg)
340
+ end
341
+ end
342
+ ret
343
+ end
344
+ GitErrorPattern = /^fatal:/
345
+ def self.error_msg_when_git_error(lines)
346
+ ret = lines.last.gsub(GitErrorPattern,'').strip()
347
+ # TODO start putting in special cases here
348
+ if ret =~ /adding files failed/
349
+ if lines.first =~ /\.git/
350
+ ret = "Cannot add files that are in a .git directory; remove any nested .git directory"
351
+ end
352
+ end
353
+ ret
354
+ end
355
+
356
+ # Method bellow show different behavior when working with 1.8.7
357
+ # so based on Hash response we know it it is:
358
+ # Hash => 1.9.3 +
359
+ # Array => 1.8.7
360
+ #
361
+
362
+ def changed
363
+ status.is_a?(Hash) ? status.changed().keys : status.changed().collect { |file| file.first }
364
+ end
365
+
366
+ def untracked
367
+ status.is_a?(Hash) ? status.untracked().keys : status.untracked().collect { |file| file.first }
368
+ end
369
+
370
+ def deleted
371
+ status.is_a?(Hash) ? status.deleted().keys : status.deleted().collect { |file| file.first }
372
+ end
373
+
374
+ def added
375
+ status.is_a?(Hash) ? status.added().keys : status.added().collect { |file| file.first }
376
+ end
377
+
378
+ def something_changed?
379
+ ![changed, untracked, deleted, added].flatten.empty?
380
+ end
381
+
382
+ def status
383
+ @git_repo.status
384
+ end
385
+
386
+ def is_there_remote?(remote_name)
387
+ @git_repo.remotes.find { |r| r.name == remote_name }
388
+ end
389
+
390
+ def any_differences?(sha1, sha2)
391
+ @git_repo.diff(sha1, sha2).size > 0
392
+ end
393
+
394
+ def git_command(cmd, opts=[])
395
+ ENV['GIT_DIR'] = "#{@git_repo.dir.path}/.git"
396
+ ENV['GIT_INDEX_FILE'] = @git_repo.index.path
397
+
398
+ path = @git_repo.dir.path
399
+
400
+ opts = [opts].flatten.join(' ')
401
+
402
+ response = `git #{cmd} #{opts}`.chomp
403
+
404
+ ENV.delete('GIT_DIR')
405
+ ENV.delete('GIT_INDEX_FILE')
406
+
407
+ return response
408
+ end
409
+
410
+ end
411
+ end
412
+ end