Class: GitJump::Actions::Jump

Inherits:
Base
  • Object
show all
Defined in:
lib/git_jump/actions/jump.rb

Overview

Action to jump to next branch or specific index

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #database, #output, #repository

Instance Method Summary collapse

Constructor Details

#initialize(index: nil) ⇒ Jump

Returns a new instance of Jump.



11
12
13
14
# File 'lib/git_jump/actions/jump.rb', line 11

def initialize(index: nil, **)
  super(**)
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



9
10
11
# File 'lib/git_jump/actions/jump.rb', line 9

def index
  @index
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git_jump/actions/jump.rb', line 16

def execute
  branches = database.list_branches(project_id)

  if branches.empty?
    output.error("No branches tracked for #{repository.project_basename}")
    output.info("Use 'git-jump add <branch>' to add branches")
    return false
  end

  target_branch = if index
                    database.branch_at_index(project_id, index.to_i)
                  else
                    current_branch = repository.current_branch
                    database.next_branch(project_id, current_branch)
                  end

  unless target_branch
    output.error("Invalid branch index: #{index}") if index
    return false
  end

  if target_branch == repository.current_branch
    output.info("Already on branch '#{target_branch}'")
    return true
  end

  repository.checkout(target_branch)
  database.add_branch(project_id, target_branch) # Update last_visited_at

  output.success("Switched to branch '#{target_branch}'")
  true
rescue StandardError => e
  output.error(e.message)
  false
end