Class: DTK::Common::GritAdapter::FileAccess
Overview
Copyright © 2010-2016 dtk contributors
This file is part of the dtk project.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Defined Under Namespace
Modules: DiffMixin, StatusMixin
Constant Summary
collapse
- TempBranch =
temp branch #TODO: make sure no name conflict
'temp_branch'
- DefaultAuthor =
{
:username => "dtk",
:email => "[email protected]"
}
Git_command__push_mutex
Instance Attribute Summary
#branch, #repo_dir
Instance Method Summary
collapse
-
#add_branch(branch) ⇒ Object
-
#add_branch?(branch) ⇒ Boolean
-
#add_file(file_rel_path, content = nil) ⇒ Object
-
#add_file_command(file_rel_path) ⇒ Object
-
#add_remove_commit_all(commit_msg) ⇒ Object
Method will add and remove all files, after commit with given msg.
-
#changed? ⇒ Boolean
Checks for changes add/delete/modified.
-
#changed_files ⇒ Object
-
#commit(commit_msg, opts = {}) ⇒ Object
-
#deleted_files ⇒ Object
-
#fetch(remote = nil) ⇒ Object
-
#find_remote_sha(ref) ⇒ Object
-
#head_commit_sha ⇒ Object
-
#merge(remote_branch_ref) ⇒ Object
-
#merge_theirs(remote_branch_ref) ⇒ Object
-
#print_status ⇒ Object
-
#pull(remote_branch, local_branch, remote = nil) ⇒ Object
-
#remove_branch(branch) ⇒ Object
-
#remove_branch?(branch) ⇒ Boolean
-
#remove_file(file_rel_path) ⇒ Object
-
#ret_merge_relationship(type, ref, opts = {}) ⇒ Object
returns :equal, :local_behind, :local_ahead, or :branchpoint type can be :remote_branch or :local_branch.
Methods included from DiffMixin
#diff
#status
#add_or_update_remote, #add_remote, #add_remote?, #branches, clone, #file_content, #initialize, #ls_r, #path_exists?, #push, #remotes
Instance Method Details
#add_branch(branch) ⇒ Object
242
243
244
245
246
|
# File 'lib/grit_adapter/file_access.rb', line 242
def add_branch(branch)
chdir_and_checkout() do
git_command(:branch,branch)
end
end
|
#add_branch?(branch) ⇒ Boolean
237
238
239
240
241
|
# File 'lib/grit_adapter/file_access.rb', line 237
def add_branch?(branch)
unless branches().include?(branch)
add_branch(branch)
end
end
|
#add_file(file_rel_path, content = nil) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/grit_adapter/file_access.rb', line 27
def add_file(file_rel_path, content=nil)
content ||= String.new
file_path = qualified_path(file_rel_path)
chdir_and_checkout do
File.open(file_path,"w"){|f|f << content}
git_command__add(file_path)
end
end
|
#add_file_command(file_rel_path) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/grit_adapter/file_access.rb', line 36
def add_file_command(file_rel_path)
chdir_and_checkout do
file_path = qualified_path(file_rel_path)
git_command__add(file_path)
end
end
|
#add_remove_commit_all(commit_msg) ⇒ Object
Method will add and remove all files, after commit with given msg
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/grit_adapter/file_access.rb', line 149
def add_remove_commit_all(commit_msg)
chdir do
changed_files().each do |c_file|
add_file_command(c_file.first)
end
deleted_files().each do |d_file|
remove_file(d_file.first)
end
commit(commit_msg)
end
end
|
#changed? ⇒ Boolean
Checks for changes add/delete/modified
142
143
144
|
# File 'lib/grit_adapter/file_access.rb', line 142
def changed?
!(changed_files() + deleted_files).empty?
end
|
#changed_files ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/grit_adapter/file_access.rb', line 91
def changed_files()
output = git_command_status()
grit_files = @grit_repo.status.files.select { |k,v| (v.type =~ /(A|M)/ || v.untracked) }
changed_files = grit_files.select do |file|
file_name = file.instance_of?(String) ? file : file.first
filter_file_through_status_output(file_name,output)
end
changed_files.to_a
end
|
#commit(commit_msg, opts = {}) ⇒ Object
164
165
166
167
168
169
170
171
172
|
# File 'lib/grit_adapter/file_access.rb', line 164
def commit(commit_msg,opts={})
cmd_args = [:commit,"-a","-m",commit_msg]
author = "#{opts[:author_username]||DefaultAuthor[:username]} <#{opts[:author_email]||DefaultAuthor[:email]}>"
cmd_args += ["--author",author]
chdir_and_checkout do
git_command(*cmd_args)
end
end
|
#deleted_files ⇒ Object
123
124
125
126
|
# File 'lib/grit_adapter/file_access.rb', line 123
def deleted_files()
@grit_repo.status.deleted().to_a
end
|
#fetch(remote = nil) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/grit_adapter/file_access.rb', line 65
def fetch(remote=nil)
remote ||= default_remote()
chdir do
git_command(:fetch,remote)
end
end
|
#find_remote_sha(ref) ⇒ Object
232
233
234
235
|
# File 'lib/grit_adapter/file_access.rb', line 232
def find_remote_sha(ref)
remote = @grit_repo.remotes.find{|r|r.name == ref}
remote && remote.commit.id
end
|
#head_commit_sha ⇒ Object
228
229
230
231
|
# File 'lib/grit_adapter/file_access.rb', line 228
def head_commit_sha()
head = @grit_repo.heads.find{|r|r.name == @branch}
head && head.commit.id
end
|
#merge(remote_branch_ref) ⇒ Object
85
86
87
88
89
|
# File 'lib/grit_adapter/file_access.rb', line 85
def merge(remote_branch_ref)
chdir_and_checkout do
git_command(:merge,remote_branch_ref)
end
end
|
#merge_theirs(remote_branch_ref) ⇒ Object
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/grit_adapter/file_access.rb', line 74
def merge_theirs(remote_branch_ref)
chdir do
git_command(:checkout,"-b",TempBranch,remote_branch_ref)
git_command(:merge,@branch,"-s","ours")
git_command(:checkout,@branch)
git_command(:reset,"--hard",TempBranch)
git_command(:branch,"-D",TempBranch)
end
end
|
#print_status ⇒ Object
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/grit_adapter/file_access.rb', line 128
def print_status()
changes = [@grit_repo.status.changed(), @grit_repo.status.untracked(), @grit_repo.status.deleted()]
puts "\nModified files:\n".colorize(:green) unless changes[0].empty?
changes[0].each { |item| puts "\t#{item.first}" }
puts "\nAdded files:\n".colorize(:yellow) unless changes[1].empty?
changes[1].each { |item| puts "\t#{item.first}" }
puts "\nDeleted files:\n".colorize(:red) unless changes[2].empty?
changes[2].each { |item| puts "\t#{item.first}" }
puts ""
end
|
#pull(remote_branch, local_branch, remote = nil) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/grit_adapter/file_access.rb', line 58
def pull(remote_branch,local_branch,remote=nil)
remote ||= default_remote()
chdir do
git_command(:pull,remote,"#{remote_branch}:#{local_branch}")
end
end
|
#remove_branch(branch) ⇒ Object
252
253
254
255
256
257
|
# File 'lib/grit_adapter/file_access.rb', line 252
def remove_branch(branch)
checkout_branch = @branch
chdir_and_checkout(checkout_branch,:stay_on_checkout_branch => true) do
git_command(:branch,"-d",branch)
end.first
end
|
#remove_branch?(branch) ⇒ Boolean
247
248
249
250
251
|
# File 'lib/grit_adapter/file_access.rb', line 247
def remove_branch?(branch)
if branches().include?(branch)
remove_branch(branch)
end
end
|
#remove_file(file_rel_path) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/grit_adapter/file_access.rb', line 49
def remove_file(file_rel_path)
file_path = qualified_path(file_rel_path)
chdir_and_checkout do
if File.exists?(file_path)
git_command(:rm,file_path)
end
end
end
|
#ret_merge_relationship(type, ref, opts = {}) ⇒ Object
returns :equal, :local_behind, :local_ahead, or :branchpoint type can be :remote_branch or :local_branch
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/grit_adapter/file_access.rb', line 180
def ret_merge_relationship(type,ref,opts={})
if (type == :remote_branch and opts[:fetch_if_needed])
fetch(ref.split("/").first)
end
other_grit_ref =
case type
when :remote_branch
@grit_repo.remotes.find{|r|r.name == ref}
when :local_branch
@grit_repo.heads.find{|r|r.name == ref}
else
raise Error.new("Illegal type parameter (#{type}) passed to ret_merge_relationship")
end
local_sha = head_commit_sha()
if opts[:ret_commit_shas]
opts[:ret_commit_shas][:local_sha] = local_sha
end
unless other_grit_ref
if type == :remote_branch
return :no_remote_ref
end
raise Error.new("Cannot find git ref (#{ref})")
end
other_sha = other_grit_ref.commit.id
if opts[:ret_commit_shas]
opts[:ret_commit_shas][:other_sha] = other_sha
end
if other_sha == local_sha
:equal
else
unless any_diffs?(local_sha,other_sha)
return :equal
end
if git_command__rev_list_contains?(local_sha,other_sha) then :local_ahead
elsif git_command__rev_list_contains?(other_sha,local_sha) then :local_behind
else :branchpoint
end
end
end
|