Merge pull request #947 from fr0m-scratch/add-sandbox-api

luasandbox: added two apis for `luasandbox`
This commit is contained in:
patzielinski 2025-10-24 03:15:10 +00:00 committed by GitHub
commit e1dfa9d0ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 1 deletions

12
docs/sandbox/README.md generated
View file

@ -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`

View file

@ -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
},
}
}

View file

@ -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 {