Class: DTK::Common::GritAdapter::FileAccess

Inherits:
DTK::Common::GritAdapter show all
Includes:
DiffMixin, StatusMixin
Defined in:
lib/grit_adapter/file_access.rb,
lib/grit_adapter/file_access/diff.rb,
lib/grit_adapter/file_access/status.rb

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]"
}

Constants inherited from DTK::Common::GritAdapter

Git_command__push_mutex

Instance Attribute Summary

Attributes inherited from DTK::Common::GritAdapter

#branch, #repo_dir

Instance Method Summary collapse

Methods included from DiffMixin

#diff

Methods included from StatusMixin

#status

Methods inherited from DTK::Common::GritAdapter

#add_or_update_remote, #add_remote, #add_remote?, #branches, clone, #file_content, #initialize, #ls_r, #path_exists?, #push, #remotes

Constructor Details

This class inherits a constructor from DTK::Common::GritAdapter

Instance Method Details

#add_branch(branch) ⇒ Object



225
226
227
228
229
# File 'lib/grit_adapter/file_access.rb', line 225

def add_branch(branch)
  chdir_and_checkout() do 
    git_command(:branch,branch)
  end
end

#add_branch?(branch) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
# File 'lib/grit_adapter/file_access.rb', line 220

def add_branch?(branch)
  unless branches().include?(branch)
    add_branch(branch)
  end
end

#add_file(file_rel_path, content = nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/grit_adapter/file_access.rb', line 10

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



19
20
21
22
23
24
# File 'lib/grit_adapter/file_access.rb', line 19

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/grit_adapter/file_access.rb', line 132

def add_remove_commit_all(commit_msg)
  chdir do
    # modified, untracked
    changed_files().each do |c_file|
      add_file_command(c_file.first)
    end
    # deleted
    deleted_files().each do |d_file|
      remove_file(d_file.first)
    end
    # commit 
    commit(commit_msg)
  end
end

#changed?Boolean

Checks for changes add/delete/modified

Returns:

  • (Boolean)


125
126
127
# File 'lib/grit_adapter/file_access.rb', line 125

def changed?
  !(changed_files() + deleted_files).empty?
end

#changed_filesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/grit_adapter/file_access.rb', line 74

def changed_files()
  # NOTE: There is issue with grit and git. Where grit.status will report file changed (modified)
  # and git status will not. Grit registers changing file time-stamp as change while git doesn't. This would 
  # not be a problem but `git push` will fail because of this. Following is fix for that.
  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

  # returns array of arrays (first element name of file)
  changed_files.to_a
end

#commit(commit_msg, opts = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/grit_adapter/file_access.rb', line 147

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
    #note using following because silent failure @grit_repo.commit_all(commit_msg)
    git_command(*cmd_args)
  end
end

#deleted_filesObject



106
107
108
109
# File 'lib/grit_adapter/file_access.rb', line 106

def deleted_files()
  # returns array of arrays (first element name of file)
  @grit_repo.status.deleted().to_a 
end

#fetch(remote = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/grit_adapter/file_access.rb', line 48

def fetch(remote=nil)
  remote ||= default_remote()
  chdir do
    git_command(:fetch,remote)
  end
end

#find_remote_sha(ref) ⇒ Object



215
216
217
218
# File 'lib/grit_adapter/file_access.rb', line 215

def find_remote_sha(ref)
  remote = @grit_repo.remotes.find{|r|r.name == ref}
  remote && remote.commit.id
end

#head_commit_shaObject



211
212
213
214
# File 'lib/grit_adapter/file_access.rb', line 211

def head_commit_sha()
  head = @grit_repo.heads.find{|r|r.name == @branch}
  head && head.commit.id
end

#merge(remote_branch_ref) ⇒ Object



68
69
70
71
72
# File 'lib/grit_adapter/file_access.rb', line 68

def merge(remote_branch_ref)
  chdir_and_checkout do
    git_command(:merge,remote_branch_ref)
  end
end

#merge_theirs(remote_branch_ref) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/grit_adapter/file_access.rb', line 57

def merge_theirs(remote_branch_ref)
  #since there is no 'git merge -s theirs' we need to simulate it
  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


111
112
113
114
115
116
117
118
119
120
# File 'lib/grit_adapter/file_access.rb', line 111

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



41
42
43
44
45
46
# File 'lib/grit_adapter/file_access.rb', line 41

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



235
236
237
238
239
240
# File 'lib/grit_adapter/file_access.rb', line 235

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

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/grit_adapter/file_access.rb', line 230

def remove_branch?(branch)
  if branches().include?(branch)
    remove_branch(branch)
  end
end

#remove_file(file_rel_path) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/grit_adapter/file_access.rb', line 32

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



163
164
165
166
167
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
209
# File 'lib/grit_adapter/file_access.rb', line 163

def ret_merge_relationship(type,ref,opts={})
  if (type == :remote_branch and opts[:fetch_if_needed])
    #TODO: this fetches all branches on the remote; see if anyway to just fetch a specfic branch
    #ref will be of form remote_name/branch
    #TODO: also see if more efficient to use git ls-remote
    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
    #shas can be different but  they can have same content so do a git diff
    unless any_diffs?(local_sha,other_sha)
      return :equal
    end
    #TODO: see if missing or mis-categorizing any condition below
    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