Sha256: bee35328330fd5f413993df63a74220368f3105d285893d8a363e7ecaaf7f8b3

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

require 'git'

class Yarii::Repository
  attr_accessor :repo_dir, :git, :remote, :branch

  Changeset = Struct.new(:total, :added, :modified, :deleted, keyword_init: true)

  def initialize(repo_dir)
    @repo_dir = repo_dir
    @git = Git.open(@repo_dir)
    @remote = "origin"
    @branch = @git.current_branch
  end

  def add(filepath)
    @git.add(filepath)
  end

  def remove(filepath)
    @git.remove(filepath)
  end

  def changes(only_indexed: true)
    indexed_flag = only_indexed ? '--cached' : nil
    git_diff = @git.lib.diff_name_status('HEAD', indexed_flag)
    Changeset.new(
      total: git_diff.length,
      added: git_diff.select{|k,v| v == 'A'}.keys,
      modified: git_diff.select{|k,v| v == 'M'}.keys,
      deleted: git_diff.select{|k,v| v == 'D'}.keys
    )
  end

  def pull(remote: nil)
    @git.pull(@remote || remote, @branch)
  end

  def needs_pull?(remote: nil)
    @git.fetch(@remote || remote)
    @git.lib.diff_name_status('HEAD','@{upstream}').length > 0
  end

  def commit(message:)
    @git.commit(message)
  end

  def push(remote: nil)
    @git.push(@remote || remote, @branch)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
yarii-editor-0.4.8 app/models/yarii/repository.rb
yarii-editor-0.4.7 app/models/yarii/repository.rb
yarii-editor-0.4.6 app/models/yarii/repository.rb
yarii-editor-0.4.5 app/models/yarii/repository.rb
yarii-editor-0.4.4 app/models/yarii/repository.rb
yarii-editor-0.4.3 app/models/yarii/repository.rb
yarii-editor-0.4.2 app/models/yarii/repository.rb
yarii-editor-0.4.1 app/models/yarii/repository.rb
yarii-editor-0.4.0 app/models/yarii/repository.rb