# class Gitlab::FileEditor

GitLab file editor

It gives you ability to make changes to files & commit this changes from GitLab UI.

### Attributes

project[RW]
ref[RW]
user[RW]

### Public Class Methods

new(user, project, ref) click to toggle source

    # File lib/gitlab/file_editor.rb, line 9
    def initialize(user, project, ref)
      self.user = user
      self.project = project
      self.ref = ref
    end

### Public Instance Methods

update(path, content, commit_message, last_commit) click to toggle source

    # File lib/gitlab/file_editor.rb, line 15
    def update(path, content, commit_message, last_commit)
      return false unless can_edit?(path, last_commit)

      Grit::Git.with_timeout(10.seconds) do
        lock_file = Rails.root.join("tmp", "#{project.path}.lock")

        File.open(lock_file, "w+") do |f|
          f.flock(File::LOCK_EX)

          unless project.satellite.exists?
            raise "Satellite doesn't exist"
          end

          project.satellite.clear

          Dir.chdir(project.satellite.path) do
            r = Grit::Repo.new('.')
            r.git.sh "git reset --hard"
            r.git.sh "git fetch origin"
            r.git.sh "git config user.name \"#{user.name}\""
            r.git.sh "git config user.email \"#{user.email}\""
            r.git.sh "git checkout -b #{ref} origin/#{ref}"
            File.open(path, 'w'){|f| f.write(content)}
            r.git.sh "git add ."
            r.git.sh "git commit -am '#{commit_message}'"
            output = r.git.sh "git push origin #{ref}"

            if output =~ %rreject/
              return false
            end
          end
        end
      end
      true
    end

### Protected Instance Methods

can_edit?(path, last_commit) click to toggle source

    # File lib/gitlab/file_editor.rb, line 53
    def can_edit?(path, last_commit)
      current_last_commit = @project.last_commit_for(ref, path).sha
      last_commit == current_last_commit
    end