class Git::Copilot::CLI

Public Instance Methods

init() click to toggle source
# File lib/git/copilot/cli.rb, line 12
def init
  if File.exist?(config_file_path) && !options[:force]
    return say_status "ERROR", "Configuration file already exists", :red
  end

  say "Writing configuration file to #{config_file_path}"
  File.write(config_file_path, empty_configuration)

  if yes?("Set up a global git alias? (This will allow you to run `git copilot`)")
    system("git config --global alias.copilot '\!git-copilot'")
  end

  solo
end
pair(*usernames) click to toggle source
# File lib/git/copilot/cli.rb, line 37
def pair(*usernames)
  authors = usernames.map do |username|
    users.fetch(username) do
      say_status "WARNING", "Unknown user #{username}", :yellow
      next
    end
  end.compact

  if authors.empty?
    return say_status "ERROR", "No users to pair with. " \
      "Did you mean to run git-copilot solo?", :red
  end

  self.current_pairs = authors
  commit_config
  write_template
  set_git_commit_template

  status
end
solo() click to toggle source
# File lib/git/copilot/cli.rb, line 28
def solo
  clear_pairs
  commit_config
  write_template
  set_git_commit_template
  status
end
status() click to toggle source
# File lib/git/copilot/cli.rb, line 59
def status
  if current_pairs.empty?
    say "Now working solo", :green
  else
    say "Now working with #{pluralize_pairs(current_pairs.length)}:"
    current_pairs.each do |author|
      say format("%{name} <%{email}>", name: author.name, email: author.email), :green
    end
  end
end

Private Instance Methods

empty_configuration() click to toggle source
# File lib/git/copilot/cli.rb, line 91
def empty_configuration
  YAML.dump(
    "template" => "# Write your commit message here\n\n%{coauthors}",
    "users" => {},
  )
end
pluralize_pairs(number) click to toggle source
# File lib/git/copilot/cli.rb, line 98
def pluralize_pairs(number)
  number == 1 ? "1 pair" : "#{number} pairs"
end
set_git_commit_template() click to toggle source
# File lib/git/copilot/cli.rb, line 79
def set_git_commit_template
  `git config commit.template #{commit_message_template_path}`
end
template(authors: []) click to toggle source
# File lib/git/copilot/cli.rb, line 83
def template(authors: [])
  coauthored_by_lines = authors.map do |user|
    format("Co-authored-by: %{name} <%{email}>", name: user.name, email: user.email)
  end.join("\n")

  format(configuration["template"], coauthors: coauthored_by_lines)
end
write_template() click to toggle source
# File lib/git/copilot/cli.rb, line 75
def write_template
  File.write(commit_message_template_path, template(authors: current_pairs))
end