diff --git a/runtime/plugins/status/help/status.md b/runtime/plugins/status/help/status.md index 6b18613d..d500f3e4 100644 --- a/runtime/plugins/status/help/status.md +++ b/runtime/plugins/status/help/status.md @@ -8,8 +8,10 @@ those options (`> help options`) for more information. This plugin provides functions that can be used in the status line format: -* `status.branch`: returns the name of the current git branch. -* `status.hash`: returns the hash of the current git commit. +* `status.branch`: returns the name of the current git branch in the repository + where the file is located. +* `status.hash`: returns the hash of the current git commit in the repository + where the file is located. * `status.paste`: returns "" if the paste option is disabled and "PASTE" if it is enabled. * `status.lines`: returns the number of lines in the buffer. diff --git a/runtime/plugins/status/status.lua b/runtime/plugins/status/status.lua index ae1f77a6..d7d4e85f 100644 --- a/runtime/plugins/status/status.lua +++ b/runtime/plugins/status/status.lua @@ -3,7 +3,10 @@ VERSION = "1.0.0" local micro = import("micro") local buffer = import("micro/buffer") local config = import("micro/config") +local shell = import("micro/shell") +local filepath = import("filepath") local humanize = import("humanize") +local strings = import("strings") function init() micro.SetStatusInfoFn("status.branch") @@ -32,30 +35,23 @@ function size(b) return humanize.Bytes(b:Size()) end -function branch(b) +local function parseRevision(b, opt) if b.Type.Kind ~= buffer.BTInfo then - local shell = import("micro/shell") - local strings = import("strings") - - local branch, err = shell.ExecCommand("git", "rev-parse", "--abbrev-ref", "HEAD") + local dir = filepath.Dir(b.Path) + local str, err = shell.ExecCommand("git", "-C", dir, "rev-parse", opt, "HEAD") if err == nil then - return strings.TrimSpace(branch) + return strings.TrimSpace(str) end - return "" end + return "" +end + +function branch(b) + return parseRevision(b, "--abbrev-ref") end function hash(b) - if b.Type.Kind ~= 5 then - local shell = import("micro/shell") - local strings = import("strings") - - local hash, err = shell.ExecCommand("git", "rev-parse", "--short", "HEAD") - if err == nil then - return strings.TrimSpace(hash) - end - return "" - end + return parseRevision(b, "--short") end function paste(b)