Class: Worktree::Command::CherryPick

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree/command/cherry_pick.rb

Instance Method Summary collapse

Constructor Details

#initialize(commit, to:, project_dir:) ⇒ CherryPick

Returns a new instance of CherryPick.



9
10
11
12
13
14
# File 'lib/worktree/command/cherry_pick.rb', line 9

def initialize(commit, to:, project_dir:)
  @commit = commit[0..7] # short commit
  @branch_remote = to
  @branch = "cherry-pick-#{@commit}-to-#{@branch_remote.tr('/', '-')}"
  @project_dir = project_dir.chomp('/')
end

Instance Method Details

#do!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/worktree/command/cherry_pick.rb', line 16

def do!
  raise "Folder #{@branch} already exists!" if Dir.exist?("#{@project_dir}/#{@branch}")
  raise 'No master repo found!' unless Dir.exist?("#{@project_dir}/master/.git")

  # fetch all
  git.remotes.each(&:fetch)

  Worktree.run_command "git worktree add -b #{@branch} ../#{@branch} #{@branch_remote}", chdir: "#{@project_dir}/master"

  begin
    Worktree.run_command "git cherry-pick #{@commit} -m 1", chdir: "#{@project_dir}/#{@branch}"
  rescue Worktree::Error => e
    # bypass conflicts while cherry-picking
    Worktree.logger.warn { e.message }
  end

  copy_files
  clone_dbs
  tmux
end