Skip to content

Commit

Permalink
Merge branch 'master' into fix/non-default-repo-pr-checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 authored Jan 31, 2025
2 parents 7df20c5 + 5594213 commit 4438cbf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
17 changes: 17 additions & 0 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ local config = require "octo.config"
local colors = require "octo.ui.colors"
local vim = vim

-- a global variable where command handlers can access the details of the last
-- command ran.
--
-- this came into existence since some commands like "comment add" need to
-- understand the line range the comment should be created on.
-- this is problematic without the command options as you exit visual mode when
-- enterting the command line.
OctoLastCmdOpts = nil

local M = {}

local get_current_buffer = function()
Expand All @@ -32,7 +41,9 @@ end

function M.setup()
vim.api.nvim_create_user_command("Octo", function(opts)
OctoLastCmdOpts = opts
require("octo.commands").octo(unpack(opts.fargs))
OctoLastCmdOpts = nil
end, { complete = require("octo.completion").octo_command_complete, nargs = "*", range = true })
local conf = config.values

Expand Down Expand Up @@ -205,6 +216,11 @@ function M.setup()
local bufnr = vim.api.nvim_get_current_buf()
local buffer = octo_buffers[bufnr]
if not buffer or not buffer:isPullRequest() then
picker.prs {
cb = function(selected)
utils.checkout_pr(selected.obj.number)
end,
}
return
end
if not utils.in_pr_repo() then
Expand Down Expand Up @@ -536,6 +552,7 @@ function M.octo(object, action, ...)
utils.error(action and "Incorrect action: " .. action or "No action specified")
return
end

res = pcall(a, ...)
if not res then
utils.error(action and "Failed action: " .. action)
Expand Down
1 change: 1 addition & 0 deletions lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local env_vars = {
http_proxy = vim.env["http_proxy"],
https_proxy = vim.env["https_proxy"],
no_proxy = vim.env["no_proxy"],
SSH_AUTH_SOCK = vim.env["SSH_AUTH_SOCK"],
}

local function get_env()
Expand Down
10 changes: 10 additions & 0 deletions lua/octo/pickers/fzf-lua/pickers/prs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ local function checkout_pull_request(entry)
utils.checkout_pr(entry.obj.number)
end

local function not_implemented()
utils.error "Not implemented yet"
end

return function(opts)
opts = opts or {}
if not opts.states then
opts.states = "OPEN"
end

if opts.cb ~= nil then
not_implemented()
return
end

local filter = picker_utils.get_filter(opts, "pull_request")
if utils.is_blank(opts.repo) then
opts.repo = utils.get_remote_name()
Expand Down
16 changes: 11 additions & 5 deletions lua/octo/reviews/file-entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ M._null_buffer = {}
---@field right_binary boolean|nil
---@field left_bufid integer
---@field right_bufid integer
--- If this table is empty, the buffer is not ready to be displayed
--- If the file is actually empty for the revision, table will be filled with a single empty line
---@field left_lines string[]
--- If this table is empty, the buffer is not ready to be displayed
--- If the file is actually empty for the revision, table will be filled with a single empty line
---@field right_lines string[]
--- If either left_fetched or right_fetched is false,
--- the buffer is not ready to be displayed.
---@field left_fetched boolean
---@field right_fetched boolean
---@field left_winid number
---@field right_winid number
---@field left_comment_ranges table
Expand Down Expand Up @@ -78,6 +78,8 @@ function FileEntry:new(opt)
right_binary = opt.right_binary,
left_lines = {},
right_lines = {},
left_fetched = false,
right_fetched = false,
diffhunks = diffhunks,
associated_bufs = {},
viewed_state = pr.files[opt.path],
Expand Down Expand Up @@ -205,21 +207,25 @@ function FileEntry:fetch()
if self.pull_request.local_right then
utils.get_file_at_commit(right_path, right_sha, function(lines)
self.right_lines = lines
self.right_fetched = true
end)
else
utils.get_file_contents(self.pull_request.repo, right_abbrev, right_path, function(lines)
self.right_lines = lines
self.right_fetched = true
end)
end

-- fetch left version
if self.pull_request.local_left then
utils.get_file_at_commit(left_path, left_sha, function(lines)
self.left_lines = lines
self.left_fetched = true
end)
else
utils.get_file_contents(self.pull_request.repo, left_abbrev, left_path, function(lines)
self.left_lines = lines
self.left_fetched = true
end)
end

Expand All @@ -232,7 +238,7 @@ end
---Determines whether the file content has been loaded and the file is ready to render
---@return boolean
function FileEntry:is_ready_to_render()
return #self.left_lines > 0 and #self.right_lines > 0
return self.left_fetched and self.right_fetched
end

---Load the buffers.
Expand Down
9 changes: 8 additions & 1 deletion lua/octo/reviews/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,15 @@ function Review:add_comment(isSuggestion)
return
end

-- get visual selected line range
-- get visual selected line range, used if coming from a keymap where current
-- mode can be evaluated.
local line1, line2 = utils.get_lines_from_context "visual"
-- if we came from the command line the command options will provide line
-- range
if OctoLastCmdOpts ~= nil then
line1 = OctoLastCmdOpts.line1
line2 = OctoLastCmdOpts.line2
end

local comment_ranges, current_bufnr
if split == "RIGHT" then
Expand Down
25 changes: 17 additions & 8 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ end
--
-- this is useful when you want to determine if the currently checked out branch
-- maps to a PR HEAD, when 'gh pr checkout' is used.
function M.get_upstream_branch_from_config()
function M.get_upstream_branch_from_config(pr)
local branch_cmd = "git rev-parse --abbrev-ref HEAD"
local branch = vim.fn.system(branch_cmd)
if vim.v.shell_error ~= 0 then
Expand Down Expand Up @@ -515,19 +515,28 @@ function M.get_upstream_branch_from_config()

local upstream_branch_ref = merge_config_kv[2]

-- remove the prefix /refs/heads/ from upstream_branch_ref resulting in
-- branch's name.
local upstream_branch_name = string.gsub(upstream_branch_ref, "^refs/heads/", "")
-- branch config can be in "refs/pull/{pr_number}/head" format
if string.find(upstream_branch_ref, "^refs/pull/") then
local pr_number = vim.split(upstream_branch_ref, "/")[3]
-- tonumber handles any whitespace/quoting issues
if tonumber(pr_number) == tonumber(pr.number) then
return branch
end
else
-- branch config can also be in "refs/heads/{upstream_branch_name} format"
local upstream_branch_name = string.gsub(upstream_branch_ref, "^refs/heads/", "")
return upstream_branch_name
end

return upstream_branch_name
return ""
end

-- Determines if we are locally in a branch matting the pr head ref when
-- the remote and branch information is stored in the branch's git config values
-- The gh CLI tool stores remote info directly in {branch.{branch}.x} configuration
-- fields and does not create a remote
function M.in_pr_branch_config_tracked(pr)
return M.get_upstream_branch_from_config():lower() == pr.head_ref_name
return M.get_upstream_branch_from_config(pr):lower() == pr.head_ref_name
end

--- Determines if we are locally are in a branch matching the pr head ref
Expand Down Expand Up @@ -1643,8 +1652,8 @@ function M.get_lines_from_context(calling_context)
line_number_start = vim.fn.line "."
line_number_end = line_number_start
elseif calling_context == "visual" then
line_number_start = vim.fn.line "'<"
line_number_end = vim.fn.line "'>"
line_number_start = vim.fn.line "v"
line_number_end = vim.fn.line "."
elseif calling_context == "motion" then
line_number_start = vim.fn.getpos("'[")[2]
line_number_end = vim.fn.getpos("']")[2]
Expand Down

0 comments on commit 4438cbf

Please sign in to comment.