Skip to content

Commit 2ae9812

Browse files
authored
Merge pull request micro-editor#3673 from niten94/status-pass-repodir
`status.lua`: Display commit and branch of repository where file is located
2 parents 3c68655 + 85e2b3b commit 2ae9812

2 files changed

Lines changed: 17 additions & 19 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: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ VERSION = "1.0.0"
33
local micro = import("micro")
44
local buffer = import("micro/buffer")
55
local config = import("micro/config")
6+
local shell = import("micro/shell")
7+
local filepath = import("filepath")
68
local humanize = import("humanize")
9+
local strings = import("strings")
710

811
function init()
912
micro.SetStatusInfoFn("status.branch")
@@ -32,30 +35,23 @@ function size(b)
3235
return humanize.Bytes(b:Size())
3336
end
3437

35-
function branch(b)
38+
local function parseRevision(b, opt)
3639
if b.Type.Kind ~= buffer.BTInfo then
37-
local shell = import("micro/shell")
38-
local strings = import("strings")
39-
40-
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")
4142
if err == nil then
42-
return strings.TrimSpace(branch)
43+
return strings.TrimSpace(str)
4344
end
44-
return ""
4545
end
46+
return ""
4647
end
4748

48-
function hash(b)
49-
if b.Type.Kind ~= 5 then
50-
local shell = import("micro/shell")
51-
local strings = import("strings")
49+
function branch(b)
50+
return parseRevision(b, "--abbrev-ref")
51+
end
5252

53-
local hash, err = shell.ExecCommand("git", "rev-parse", "--short", "HEAD")
54-
if err == nil then
55-
return strings.TrimSpace(hash)
56-
end
57-
return ""
58-
end
53+
function hash(b)
54+
return parseRevision(b, "--short")
5955
end
6056

6157
function paste(b)

0 commit comments

Comments
 (0)