Method: Tetra::Project#merge_new_content

Defined in:
lib/tetra/project.rb

#merge_new_content(new_content, path, comment, kind) ⇒ Object

replaces content in path with new_content, commits using comment and 3-way merges new and old content with the previous version of file of the same kind, if it exists. returns the number of conflicts



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tetra/project.rb', line 110

def merge_new_content(new_content, path, comment, kind)
  from_directory do
    log.debug "merging new content to #{path} of kind #{kind}"
    already_existing = File.exist?(path)

    generated_comment = "tetra: generated-#{kind}"
    whole_comment = [comment, generated_comment].join("\n\n")

    if already_existing
      unless @git.latest_id(generated_comment)
        log.debug "committing new file"
        @git.commit_file(path, whole_comment)
      end
      log.debug "moving #{path} to #{path}.tetra_user_edited"
      File.rename(path, "#{path}.tetra_user_edited")
    end

    previous_id = @git.latest_id(generated_comment)

    File.open(path, "w") { |io| io.write(new_content) }
    log.debug "committing new content: #{comment}"
    @git.commit_file(path, whole_comment)

    if already_existing
      # 3-way merge
      conflict_count = @git.merge_with_id(path, "#{path}.tetra_user_edited", previous_id)
      File.delete("#{path}.tetra_user_edited")

      @git.commit_file(path, "User changes merged back") if conflict_count == 0

      return conflict_count
    end
    return 0
  end
end