class LearnTest::Git::Wip::Base

Constants

TEMPFILE

Attributes

wip_branch[R]
working_branch[R]

Public Class Methods

new(base:, message:) click to toggle source
# File lib/learn_test/git/wip/base.rb, line 16
def initialize(base:, message:)
  @base = base
  @message = message
  @success = nil

  current_branch = @base.current_branch

  raise Errors::NoCommitsError, 'master' if current_branch.nil? # TODO: Swap to `main`?

  @tmp = Tempfile.new(TEMPFILE)
  @working_branch = Branch.new(base: @base, name: current_branch)
  @wip_branch = Reference.new(base: @base, name: current_branch)
end

Public Instance Methods

process!() click to toggle source
# File lib/learn_test/git/wip/base.rb, line 30
def process!
  if @wip_branch.last_revision
    merge = @base.merge_base(@wip_branch.last_revision, @working_branch.last_revision)

    @wip_branch.parent = if merge == @working_branch.last_revision
                           @wip_branch.last_revision
                         else
                           @working_branch.last_revision
                         end
  else
    @wip_branch.parent = @working_branch.last_revision
  end

  new_tree = build_new_tree(@wip_branch.parent)
  @base.diff(new_tree, @wip_branch.parent)

  commit = @base.commit_tree(new_tree, parent: @wip_branch.parent)

  @base.lib.send(:command, 'update-ref', ['-m', @message, @wip_branch, commit.objectish])

  @success = true
ensure
  cleanup
end
success?() click to toggle source
# File lib/learn_test/git/wip/base.rb, line 55
def success?
  @success
end

Private Instance Methods

build_new_tree(wip_parent) click to toggle source
# File lib/learn_test/git/wip/base.rb, line 61
def build_new_tree(wip_parent)
  index = "#{@tmp.path}-index"

  FileUtils.rm(index, force: true)
  FileUtils.cp("#{@base.dir.path}/.git/index", index)

  @base.read_tree(wip_parent)
  @base.lib.send(:command, 'add', ['--update', '--', '.'])
  @base.add(all: true)

  new_tree_obj = @base.write_tree

  FileUtils.rm(index, force: true)

  new_tree_obj
end
cleanup() click to toggle source
# File lib/learn_test/git/wip/base.rb, line 78
def cleanup
  FileUtils.rm("#{@tmp.path}-*", force: true)
end