Sha256: 2836e963d7aecfe37d5f4af4472aa0ae1dcb898e4a3029387a09b2479222f268

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 KB

Contents

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

module Gitx
  module Cli
    class StartCommand < BaseCommand
      EXAMPLE_BRANCH_NAMES = %w( api-fix-invalid-auth desktop-cleanup-avatar-markup share-form-add-edit-link )
      VALID_BRANCH_NAME_REGEX = /^[A-Za-z0-9\-_]+$/

      desc 'start', 'start a new git branch with latest changes from master'
      method_option :issue, type: :numeric, aliases: '-i', desc: 'Github issue number'
      def start(branch_name = nil)
        until valid_new_branch_name?(branch_name)
          branch_name = ask("What would you like to name your branch? (ex: #{EXAMPLE_BRANCH_NAMES.sample})")
        end

        checkout_branch Gitx::BASE_BRANCH
        run_cmd 'git pull'
        repo.create_branch branch_name, Gitx::BASE_BRANCH
        checkout_branch branch_name
        run_cmd(%Q(git commit -m "Starting work on #{branch_name} (Issue ##{options[:issue]})" --allow-empty)) if options[:issue]
      end

      private

      def valid_new_branch_name?(branch)
        return false if repo_branches.include?(branch)
        branch =~ VALID_BRANCH_NAME_REGEX
      end

      def repo_branches
        @branch_names ||= repo.branches.each_name.map do |branch|
          branch.split('/').last
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gitx-2.20.0 lib/gitx/cli/start_command.rb
gitx-2.20.0.ci.117.1 lib/gitx/cli/start_command.rb
gitx-2.19.0.ci.114.1 lib/gitx/cli/start_command.rb
gitx-2.19.0.ci.112.1 lib/gitx/cli/start_command.rb
gitx-2.19.0 lib/gitx/cli/start_command.rb
gitx-2.18.0.ci.106.1 lib/gitx/cli/start_command.rb
gitx-2.18.0 lib/gitx/cli/start_command.rb
gitx-2.18.0.pre.ci.104.1 lib/gitx/cli/start_command.rb
gitx-2.18.0.pre lib/gitx/cli/start_command.rb