diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 8ce2ff3..344b0cd 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,5 +1,3 @@ -Thanks to your contribute, while please finish below tasks: - # Regression Test ## Platforms diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d3921ac..a7368b5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -96,7 +96,12 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} version: latest - args: --config-path .stylua.toml --color always --check lua + args: --config-path .stylua.toml ./ + - name: Format with stylua + if: ${{ github.ref != 'refs/heads/master' }} + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "chore(pr): format with stylua" release: name: Release if: ${{ github.ref == 'refs/heads/master' }} diff --git a/lua/gitlinker/git.lua b/lua/gitlinker/git.lua index b48fd75..db21112 100644 --- a/lua/gitlinker/git.lua +++ b/lua/gitlinker/git.lua @@ -71,11 +71,15 @@ local function cmd(args, cwd) end --- @package ---- @return CmdResult +--- @return string[]|nil local function _get_remote() local result = cmd({ "git", "remote" }) logger.debug("|git._get_remote| result:%s", vim.inspect(result)) - return result + if type(result.stdout) ~= "table" or #result.stdout == 0 then + result:print_err("fatal: git repo has no remote") + return nil + end + return result.stdout end --- @param remote string @@ -90,7 +94,7 @@ local function get_remote_url(remote) ) if not result:has_out() then result:print_err( - "failed to get remote url by remote '" .. remote .. "'" + "fatal: failed to get remote url by remote '" .. remote .. "'" ) return nil end @@ -121,7 +125,7 @@ local function _get_rev_name(revspec) vim.inspect(result) ) if not result:has_out() then - result:print_err("git branch has no remote") + result:print_err("fatal: git branch has no remote") return nil end return result.stdout[1] @@ -140,7 +144,11 @@ local function is_file_in_rev(file, revspec) ) if result:has_err() then result:print_err( - "'" .. file .. "' does not exist in remote '" .. revspec .. "'" + "fatal: '" + .. file + .. "' does not exist in remote '" + .. revspec + .. "'" ) return false end @@ -237,7 +245,7 @@ local function get_root() vim.inspect(result) ) if not result:has_out() then - result:print_err("not in a git repository") + result:print_err("fatal: not in a git repository") return nil end return result.stdout[1] @@ -246,16 +254,13 @@ end --- @return string? local function get_branch_remote() -- origin/upstream - --- @type CmdResult - local remote_result = _get_remote() - - if type(remote_result.stdout) ~= "table" or #remote_result.stdout == 0 then - remote_result:print_err("git repo has no remote") + local remotes = _get_remote() + if not remotes then return nil end - if #remote_result.stdout == 1 then - return remote_result.stdout[1] + if #remotes == 1 then + return remotes[1] end -- origin/linrongbin16/add-rule2 @@ -278,7 +283,6 @@ local function get_branch_remote() return nil end - local remotes = remote_result.stdout for _, remote in ipairs(remotes) do if remote_from_upstream_branch == remote then return remote @@ -294,6 +298,10 @@ local function get_branch_remote() end local M = { + CmdResult = CmdResult, + _get_remote = _get_remote, + _get_rev = _get_rev, + _get_rev_name = _get_rev_name, get_root = get_root, get_remote_url = get_remote_url, is_file_in_rev = is_file_in_rev, diff --git a/test/git_spec.lua b/test/git_spec.lua new file mode 100644 index 0000000..4c01dd7 --- /dev/null +++ b/test/git_spec.lua @@ -0,0 +1,101 @@ +local cwd = vim.fn.getcwd() + +describe("git", function() + local assert_eq = assert.is_equal + local assert_true = assert.is_true + local assert_false = assert.is_false + + before_each(function() + vim.api.nvim_command("cd " .. cwd) + vim.opt.swapfile = false + local logger = require("gitlinker.logger") + logger.setup() + vim.cmd([[ edit lua/gitlinker.lua ]]) + end) + + local logger = require("gitlinker.logger") + logger.setup({ + level = "DEBUG", + console_log = true, + file_log = true, + }) + local git = require("gitlinker.git") + local path = require("gitlinker.path") + describe("[git]", function() + it("_get_remote", function() + local r = git._get_remote() + print(string.format("_get_remote:%s\n", vim.inspect(r))) + assert_eq(type(r), "table") + end) + it("get_remote_url", function() + local remote = git.get_branch_remote() + print(string.format("get_branch_remote:%s\n", vim.inspect(remote))) + if remote then + assert_eq(type(remote), "string") + assert_true(string.len(remote) > 0) + local r = git.get_remote_url(remote) + print(string.format("get_remote_url:%s\n", vim.inspect(r))) + assert_eq(type(r), "string") + assert_true(string.len(r) > 0) + else + assert_true(remote == nil) + end + end) + it("_get_rev(@{u})", function() + local rev = git._get_rev("@{u}") + if rev then + print(string.format("_get_rev:%s\n", vim.inspect(rev))) + assert_eq(type(rev), "string") + assert_true(string.len(rev) > 0) + else + assert_true(rev == nil) + end + end) + it("_get_rev_name(@{u})", function() + local rev = git._get_rev_name("@{u}") + if rev then + print(string.format("_get_rev_name:%s\n", vim.inspect(rev))) + assert_eq(type(rev), "string") + assert_true(string.len(rev) > 0) + else + assert_true(rev == nil) + end + end) + it("is_file_in_rev", function() + local remote = git.get_branch_remote() + if not remote then + assert_true(remote == nil) + return + end + assert_eq(type(remote), "string") + assert_true(string.len(remote) > 0) + local remote_url = git.get_remote_url(remote) + if not remote_url then + assert_true(remote_url == nil) + return + end + assert_eq(type(remote_url), "string") + assert_true(string.len(remote_url) > 0) + + local rev = git.get_closest_remote_compatible_rev(remote) --[[@as string]] + if not rev then + assert_true(rev == nil) + return + end + assert_eq(type(rev), "string") + assert_true(string.len(rev) > 0) + + local bufpath = path.buffer_relpath() --[[@as string]] + if not bufpath then + assert_true(bufpath == nil) + return + end + local actual = git.is_file_in_rev(bufpath, rev) + if actual ~= nil then + print(string.format("is_file_in_rev:%s\n", vim.inspect(actual))) + else + assert_true(actual == nil) + end + end) + end) +end) diff --git a/test/gitlinker_spec.lua b/test/gitlinker_spec.lua index 742235f..616cf09 100644 --- a/test/gitlinker_spec.lua +++ b/test/gitlinker_spec.lua @@ -7,11 +7,65 @@ describe("gitlinker", function() before_each(function() vim.api.nvim_command("cd " .. cwd) + vim.opt.swapfile = false + local logger = require("gitlinker.logger") + logger.setup() + vim.cmd([[ edit lua/gitlinker.lua ]]) end) local gitlinker = require("gitlinker") gitlinker.setup() + local Linker = require("gitlinker.linker").Linker describe("[gitlinker]", function() + it("_make_sharable_permalinks", function() + local lk1 = Linker:make({ lstart = 10, lend = 23 }) --[[@as Linker]] + if lk1 then + local actual1 = gitlinker._make_sharable_permalinks( + "https://github.com/linrongbin16/gitlinker.nvim/blob/", + lk1 + ) + assert_eq(type(actual1), "string") + assert_true(string.len(actual1) > 0) + assert_eq(actual1:sub(#actual1 - 7), "#L10-L23") + print( + string.format("make permalink1:%s\n", vim.inspect(actual1)) + ) + else + assert_true(lk1 == nil) + end + + local lk2 = Linker:make({ lstart = 17, lend = 17 }) --[[@as Linker]] + if lk2 then + local actual2 = gitlinker._make_sharable_permalinks( + "https://github.com/linrongbin16/gitlinker.nvim/blob/", + lk2 + ) + assert_eq(type(actual2), "string") + assert_true(string.len(actual2) > 0) + assert_eq(actual2:sub(#actual2 - 3), "#L17") + print( + string.format("make permalink2:%s\n", vim.inspect(actual2)) + ) + else + assert_true(lk2 == nil) + end + + local lk3 = Linker:make() --[[@as Linker]] + if lk3 then + local actual3 = gitlinker._make_sharable_permalinks( + "https://github.com/linrongbin16/gitlinker.nvim/blob/", + lk3 + ) + print( + string.format("make permalink3:%s\n", vim.inspect(actual3)) + ) + assert_eq(type(actual3), "string") + assert_true(string.len(actual3) > 0) + assert_eq(actual3:sub(#actual3 - 2), "#L1") + else + assert_true(lk3 == nil) + end + end) it("_map_remote_to_host", function() local test_cases = { -- [1]