Sha256: f433c30e43d8e0ff35938fab416b7b7ea39bb98bb70d393f4658640452cc192d

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Octodmin::Controllers::Syncs
  class Create
    include Octodmin::Action
    expose :message

    def call(params)
      self.format = :json

      site = Octodmin::Site.new
      git = Git.open(Octodmin::App.dir)

      # Add files to commit stage
      stage(site, git)

      # Pull changes
      git.pull

      # Commit and push changes if any
      commit(site, git)
    rescue Git::GitExecuteError => e
      halt 400, JSON.dump(errors: [e.message])
    ensure
      git.reset(".")
    end

    private

    def stage(site, git)
      # Posts
      deleted = site.status.deleted.keys.map { |path| File.join(Octodmin::App.dir, path) }
      site.posts.each do |post|
        path = File.join(site.source, post.path)
        git.add(path) unless deleted.include?(path)
      end

      # Uploads
      dir = File.join(site.source, "octodmin")
      git.add(dir) if Dir.exists?(dir)
    end

    def paths(site, git)
      status = site.reset.status
      keys = status.changed.keys + status.added.keys + status.deleted.keys

      keys.sort.reverse.map do |path|
        File.join(Octodmin::App.dir, path).sub(File.join(site.source, ""), "")
      end
    end

    def commit(site, git)
      staged = paths(site, git)

      if staged.any?
        @message  = "Octodmin sync for #{staged.count} file#{"s" if staged.count > 1}"
        @message += "\n\n#{staged.join("\n")}"

        git.commit(@message)
        git.push
      else
        @message = "Everything is up-to-date"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
octodmin-0.3.2 app/controllers/syncs/create.rb
octodmin-0.3.1 app/controllers/syncs/create.rb