Sha256: ff4334b8e7ad4cfe4418de31483c678e68306485f449b35fc103bf2c97a62d10

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

require 'thor'
require 'gitx'
require 'gitx/cli/base_command'

module Gitx
  module Cli
    class UpdateCommand < BaseCommand
      desc 'update', 'Update the current branch with latest changes from the remote feature branch and main'
      def update
        say 'Updating '
        say "#{current_branch.name} ", :green
        say 'with latest changes from '
        say config.base_branch, :green

        update_base_branch
        update_branch(current_branch.name) if remote_branch_exists?(current_branch.name)
        update_branch(config.base_branch, repository: '.')

        run_git_cmd 'share'
      end

      private

      def update_base_branch
        branch_name = current_branch.name
        checkout_branch(config.base_branch)
        update_branch(config.base_branch)
        checkout_branch(branch_name)
      end

      def update_branch(branch, repository: 'origin')
        run_git_cmd 'pull', repository, branch
      rescue Gitx::Executor::ExecutionError
        raise MergeError, 'Merge conflict occurred. Please fix merge conflict and rerun the command'
      end

      def remote_branch_exists?(branch)
        repo.branches.each_name(:remote).include?("origin/#{branch}")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gitx-4.4.0 lib/gitx/cli/update_command.rb
gitx-4.3.0 lib/gitx/cli/update_command.rb
gitx-4.1.1 lib/gitx/cli/update_command.rb
gitx-4.1.0 lib/gitx/cli/update_command.rb