Module: RuVim::Git::Branch::HandlerMethods

Included in:
Handler
Defined in:
lib/ruvim/git/branch.rb

Overview

Command handler methods

Instance Method Summary collapse

Instance Method Details

#git_branch(ctx) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruvim/git/branch.rb', line 43

def git_branch(ctx, **)
  file_path = git_resolve_path(ctx)
  unless file_path
    ctx.editor.echo_error("No file or directory to resolve git repo")
    return
  end

  lines, root, err = Branch.run(file_path)
  unless lines
    ctx.editor.echo_error("git branch: #{err}")
    return
  end

  buf = ctx.editor.add_virtual_buffer(
    kind: :git_branch,
    name: "[Git Branch]",
    lines: lines,
    readonly: true,
    modifiable: false
  )
  buf.options["git_repo_root"] = root
  ctx.editor.switch_to_buffer(buf.id)
  bind_git_buffer_keys(ctx.editor, buf.id)
  ctx.editor.echo("[Git Branch]")
end

#git_branch_checkout(ctx) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruvim/git/branch.rb', line 69

def git_branch_checkout(ctx, **)
  buf = ctx.buffer
  unless buf.kind == :git_branch
    ctx.editor.echo_error("Not a git branch buffer")
    return
  end

  line = buf.line_at(ctx.window.cursor_y)
  branch = Branch.parse_branch_name(line)
  unless branch
    ctx.editor.echo_error("No branch on this line")
    return
  end

  ctx.editor.enter_command_line_mode(":")
  ctx.editor.command_line.replace_text("git checkout #{branch}")
end

#git_branch_execute_checkout(ctx, argv: []) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruvim/git/branch.rb', line 87

def git_branch_execute_checkout(ctx, argv: [], **)
  branch = argv.first.to_s.strip
  raise RuVim::CommandError, "Usage: :git checkout <branch>" if branch.empty?

  root = ctx.buffer.kind == :git_branch ? ctx.buffer.options["git_repo_root"] : nil
  unless root
    file_path = git_resolve_path(ctx)
    root, err = Git.repo_root(file_path) if file_path
    raise RuVim::CommandError, "Not in a git repository" unless root
  end

  _out, err, status = Open3.capture3("git", "checkout", branch, chdir: root)
  unless status.success?
    raise RuVim::CommandError, "git checkout: #{err.strip}"
  end

  # Close git branch buffer if we're in one
  if ctx.buffer.kind == :git_branch
    ctx.editor.delete_buffer(ctx.buffer.id)
  end
  ctx.editor.echo("Switched to branch '#{branch}'")
end