Sha256: 9043499df041fc602de7fa851b4b04c71b33b31e3b434af8890a7a246448bfc4

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

require "active_support/core_ext/string/inflections"
require "cp8_cli/ci"
require "cp8_cli/github/pull_request"
require "cp8_cli/branch_name"
require "cp8_cli/current_user"
require "cp8_cli/pull_request_title"
require "cp8_cli/pull_request_body"

module Cp8Cli
  class Branch

    attr_reader :name

    def initialize(name)
      @name = name
    end

    def self.current
      new Command.read("git rev-parse --abbrev-ref HEAD")
    end

    def self.suggestion
      new("suggestion-#{SecureRandom.hex(8)}")
    end

    def checkout
      Command.run "git checkout #{name} >/dev/null 2>&1 || git checkout -b #{name}", title: "Checking out new branch"
    end

    def push
      Command.run "git push origin #{name} -u", title: "Pushing to origin"
    end

    def open_pr
      pull_request.open
    end

    def open_ci
      Ci.new(branch_name: name, repo: repo).open
    end

    def reset
      if dirty?
        Command.error "Dirty working directory, not resetting."
      else
        Command.run "git reset --hard origin/#{name}", title: "Resetting branch"
      end
    end

    def to_s
      name
    end

    private

      def repo
        Repo.current
      end

      def dirty?
        Command.read("git status --porcelain")
      end

      def pull_request
        existing_pull_request || new_pull_request
      end

      def existing_pull_request
        Github::PullRequest.find_by(branch: name, repo: repo)
      end

      def new_pull_request
        Github::PullRequest.new(from: name)
      end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cp8_cli-9.1.1 lib/cp8_cli/branch.rb
cp8_cli-9.1.0 lib/cp8_cli/branch.rb
cp8_cli-9.0.3 lib/cp8_cli/branch.rb
cp8_cli-9.0.2 lib/cp8_cli/branch.rb
cp8_cli-9.0.1 lib/cp8_cli/branch.rb
cp8_cli-9.0.0 lib/cp8_cli/branch.rb
cp8_cli-8.2.1 lib/cp8_cli/branch.rb
cp8_cli-8.2.0 lib/cp8_cli/branch.rb