Skip to content

Commit

Permalink
bug: return nil value when current dir isn't a git repo (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
csessh authored Dec 16, 2024
1 parent 90d262c commit e178902
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ doc/tags
Session.vim
.DS_Store
NUL

.editorconfig
16 changes: 16 additions & 0 deletions lua/git-dashboard-nvim/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ local null = is_windows and "NUL" or "/dev/null"

Git = {}

---@return boolean
Git.is_git_repo = function()
local handle = io.popen("git status &>" .. null .. "; echo $?")
if not handle then
return false
end

local exit_code = handle:read("*a")
handle:close()

-- git status command returns
-- 0 exit code if it is a git repository,
-- 128 exit code otherwise
return tonumber(exit_code) == 0
end

---@return string
Git.get_repo_with_owner = function()
local handle = io.popen("git remote get-url origin 2>" .. null)
Expand Down
19 changes: 15 additions & 4 deletions lua/git-dashboard-nvim/heatmap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Heatmap = {}
---@param config Config
---@return string
Heatmap.generate_heatmap = function(config)
if not Git.is_git_repo() then
return ""
end

if config.use_current_branch then
config.branch = Git.get_current_branch()
end
Expand All @@ -24,13 +28,20 @@ Heatmap.generate_heatmap = function(config)
end

local commits = Git.get_commit_dates(author, config.branch)

local current_date_info = utils.current_date_info()

local base_heatmap = HeatmapUtils.generate_base_heatmap(commits, current_date_info)
local base_heatmap = HeatmapUtils.generate_base_heatmap(
commits,
current_date_info
)

local ascii_heatmap = HeatmapUtils.generate_ascii_heatmap(
base_heatmap,
config,
repo_with_owner,
current_date_info
)

local ascii_heatmap =
HeatmapUtils.generate_ascii_heatmap(base_heatmap, config, repo_with_owner, current_date_info)
return ascii_heatmap
end

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/git_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ describe("git", function()

assert(commits ~= nil)
end)

it("should return git status exit code 0", function()
local git = require("git-dashboard-nvim.git")
local is_git_repo = git.is_git_repo()

assert(is_git_repo == true)
end)
end)

0 comments on commit e178902

Please sign in to comment.