diff --git a/docs/sandbox/README.md b/docs/sandbox/README.md index 1997cc99..d9430777 100644 --- a/docs/sandbox/README.md +++ b/docs/sandbox/README.md @@ -84,6 +84,18 @@ Retrieve the remote URL for the specified Git remote. gitGetRemoteURL("origin") -> "example.com/example/example" ``` +## gitGetStagedFilePaths + +**Signature:** `gitGetStagedFilePaths() -> paths` + +Retrieve a Lua table of file paths that have staged changes (changes in the index). + +### Example 1 + +``` +gitGetStagedFilePaths() -> ["foo/bar.txt", "baz/qux.py"] +``` + ## gitGetSymbolicReferenceTarget **Signature:** `gitGetSymbolicReferenceTarget(ref) -> ref` diff --git a/internal/luasandbox/apis.go b/internal/luasandbox/apis.go index 2bc3d5cd..1fca64d0 100644 --- a/internal/luasandbox/apis.go +++ b/internal/luasandbox/apis.go @@ -350,3 +350,35 @@ func (l *LuaEnvironment) apiGitGetRemoteURL() API { }, } } + +func (l *LuaEnvironment) apiGitGetStagedFilePaths() API { + return &GoAPI{ + Name: "gitGetStagedFilePaths", + Signature: "gitGetStagedFilePaths() -> paths", + Help: "Retrieve a Lua table of file paths that have staged changes (changes in the index).", + Examples: []string{ + "gitGetStagedFilePaths() -> [\"foo/bar.txt\", \"baz/qux.py\"]", + }, + Implementation: func(s *lua.LState) int { + statuses, err := l.repository.Status() + if err != nil { + s.Push(lua.LString(err.Error())) + return 1 + } + + resultTable := s.NewTable() + localIndex := 1 + for filePath, fileStatus := range statuses { + if fileStatus.X != gitinterface.StatusCodeUnmodified && + fileStatus.X != gitinterface.StatusCodeIgnored && + fileStatus.X != gitinterface.StatusCodeUntracked { + resultTable.RawSetInt(localIndex, lua.LString(filePath)) + localIndex++ + } + } + + s.Push(resultTable) + return 1 + }, + } +} diff --git a/internal/luasandbox/luasandbox.go b/internal/luasandbox/luasandbox.go index b64fa426..7e6449e2 100644 --- a/internal/luasandbox/luasandbox.go +++ b/internal/luasandbox/luasandbox.go @@ -105,7 +105,7 @@ func (l *LuaEnvironment) RunScript(script string, parameters lua.LTable) (int, e // If a table is returned, then this likely means that the hook didn't // return an exit code. Return a 1 for safety. - _, ok := returnValue.(*lua.LNumber) + _, ok := returnValue.(lua.LNumber) if !ok { return 1, nil } @@ -218,6 +218,7 @@ func (l *LuaEnvironment) registerAPIFunctions() error { "gitGetCommitMessage": l.apiGitGetCommitMessage(), "gitGetFilePathsChangedByCommit": l.apiGitGetFilePathsChangedByCommit(), "gitGetRemoteURL": l.apiGitGetRemoteURL(), + "gitGetStagedFilePaths": l.apiGitGetStagedFilePaths(), } for name, availableAPI := range registerAPIs {