From d992c606c5c32f3927cf9171c3ddc2075a47a7a3 Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Sat, 15 Feb 2025 23:17:33 +0800 Subject: [PATCH 1/2] status.lua: Move import lines to beginning of file --- runtime/plugins/status/status.lua | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/runtime/plugins/status/status.lua b/runtime/plugins/status/status.lua index ae1f77a6..90b2f0c1 100644 --- a/runtime/plugins/status/status.lua +++ b/runtime/plugins/status/status.lua @@ -3,7 +3,9 @@ VERSION = "1.0.0" local micro = import("micro") local buffer = import("micro/buffer") local config = import("micro/config") +local shell = import("micro/shell") local humanize = import("humanize") +local strings = import("strings") function init() micro.SetStatusInfoFn("status.branch") @@ -34,9 +36,6 @@ end function branch(b) 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") if err == nil then return strings.TrimSpace(branch) @@ -47,9 +46,6 @@ 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) From 85e2b3bd86bb5cb17283c7fd094813078cdbefea Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Sun, 2 Mar 2025 09:40:27 +0800 Subject: [PATCH 2/2] status.lua: Display hash and branch of file Return current commit hash and branch of repository where file in buffer is located instead of current directory. --- runtime/plugins/status/help/status.md | 6 ++++-- runtime/plugins/status/status.lua | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) 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 90b2f0c1..d7d4e85f 100644 --- a/runtime/plugins/status/status.lua +++ b/runtime/plugins/status/status.lua @@ -4,6 +4,7 @@ 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") @@ -34,24 +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 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 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)