Skip to content

Commit 85e2b3b

Browse files
committed
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.
1 parent d992c60 commit 85e2b3b

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

runtime/plugins/status/help/status.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ those options (`> help options`) for more information.
88

99
This plugin provides functions that can be used in the status line format:
1010

11-
* `status.branch`: returns the name of the current git branch.
12-
* `status.hash`: returns the hash of the current git commit.
11+
* `status.branch`: returns the name of the current git branch in the repository
12+
where the file is located.
13+
* `status.hash`: returns the hash of the current git commit in the repository
14+
where the file is located.
1315
* `status.paste`: returns "" if the paste option is disabled and "PASTE"
1416
if it is enabled.
1517
* `status.lines`: returns the number of lines in the buffer.

runtime/plugins/status/status.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local micro = import("micro")
44
local buffer = import("micro/buffer")
55
local config = import("micro/config")
66
local shell = import("micro/shell")
7+
local filepath = import("filepath")
78
local humanize = import("humanize")
89
local strings = import("strings")
910

@@ -34,24 +35,23 @@ function size(b)
3435
return humanize.Bytes(b:Size())
3536
end
3637

37-
function branch(b)
38+
local function parseRevision(b, opt)
3839
if b.Type.Kind ~= buffer.BTInfo then
39-
local branch, err = shell.ExecCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
40+
local dir = filepath.Dir(b.Path)
41+
local str, err = shell.ExecCommand("git", "-C", dir, "rev-parse", opt, "HEAD")
4042
if err == nil then
41-
return strings.TrimSpace(branch)
43+
return strings.TrimSpace(str)
4244
end
43-
return ""
4445
end
46+
return ""
47+
end
48+
49+
function branch(b)
50+
return parseRevision(b, "--abbrev-ref")
4551
end
4652

4753
function hash(b)
48-
if b.Type.Kind ~= 5 then
49-
local hash, err = shell.ExecCommand("git", "rev-parse", "--short", "HEAD")
50-
if err == nil then
51-
return strings.TrimSpace(hash)
52-
end
53-
return ""
54-
end
54+
return parseRevision(b, "--short")
5555
end
5656

5757
function paste(b)

0 commit comments

Comments
 (0)