2022-05-07 07:23:08 +02:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2023-04-06 11:13:42 +02:00
|
|
|
"encoding/json"
|
2023-04-16 07:43:54 +02:00
|
|
|
"fmt"
|
2022-05-07 07:23:08 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-04-16 07:43:54 +02:00
|
|
|
"strconv"
|
2022-05-07 07:23:08 +02:00
|
|
|
|
2023-04-05 19:01:49 +02:00
|
|
|
"github.com/fsmiamoto/git-todo-parser/todo"
|
2023-04-16 07:43:54 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-05-07 07:23:08 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2023-04-05 19:01:49 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2023-04-16 07:43:54 +02:00
|
|
|
"github.com/samber/lo"
|
2022-05-07 07:23:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sometimes lazygit will be invoked in daemon mode from a parent lazygit process.
|
|
|
|
// We do this when git lets us supply a program to run within a git command.
|
|
|
|
// For example, if we want to ensure that a git command doesn't hang due to
|
|
|
|
// waiting for an editor to save a commit message, we can tell git to invoke lazygit
|
|
|
|
// as the editor via 'GIT_EDITOR=lazygit', and use the env var
|
2023-04-16 07:43:54 +02:00
|
|
|
// 'LAZYGIT_DAEMON_KIND=1' (exit immediately) to specify that we want to run lazygit
|
|
|
|
// as a daemon which simply exits immediately.
|
|
|
|
//
|
|
|
|
// 'Daemon' is not the best name for this, because it's not a persistent background
|
|
|
|
// process, but it's close enough.
|
2022-05-07 07:23:08 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
type DaemonKind int
|
2022-05-07 07:23:08 +02:00
|
|
|
|
|
|
|
const (
|
2023-04-16 07:43:54 +02:00
|
|
|
// for when we fail to parse the daemon kind
|
|
|
|
DaemonKindUnknown DaemonKind = iota
|
|
|
|
|
|
|
|
DaemonKindExitImmediately
|
|
|
|
DaemonKindCherryPick
|
|
|
|
DaemonKindMoveTodoUp
|
|
|
|
DaemonKindMoveTodoDown
|
|
|
|
DaemonKindInsertBreak
|
|
|
|
DaemonKindChangeTodoActions
|
2022-05-07 07:23:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DaemonKindEnvKey string = "LAZYGIT_DAEMON_KIND"
|
2023-03-29 00:49:19 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
// Contains json-encoded arguments to the daemon
|
|
|
|
DaemonInstructionEnvKey string = "LAZYGIT_DAEMON_INSTRUCTION"
|
2023-04-06 11:13:42 +02:00
|
|
|
)
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func getInstruction() Instruction {
|
|
|
|
jsonData := os.Getenv(DaemonInstructionEnvKey)
|
2023-04-05 19:01:49 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
mapping := map[DaemonKind]func(string) Instruction{
|
|
|
|
DaemonKindExitImmediately: deserializeInstruction[*ExitImmediatelyInstruction],
|
|
|
|
DaemonKindCherryPick: deserializeInstruction[*CherryPickCommitsInstruction],
|
|
|
|
DaemonKindChangeTodoActions: deserializeInstruction[*ChangeTodoActionsInstruction],
|
|
|
|
DaemonKindMoveTodoUp: deserializeInstruction[*MoveTodoUpInstruction],
|
|
|
|
DaemonKindMoveTodoDown: deserializeInstruction[*MoveTodoDownInstruction],
|
|
|
|
DaemonKindInsertBreak: deserializeInstruction[*InsertBreakInstruction],
|
|
|
|
}
|
2022-05-07 07:23:08 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
return mapping[getDaemonKind()](jsonData)
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Handle(common *common.Common) {
|
2023-04-16 07:43:54 +02:00
|
|
|
if !InDaemonMode() {
|
2022-05-07 07:23:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
instruction := getInstruction()
|
|
|
|
|
|
|
|
if err := instruction.run(common); err != nil {
|
2022-05-07 07:23:08 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InDaemonMode() bool {
|
2023-04-16 07:43:54 +02:00
|
|
|
return getDaemonKind() != DaemonKindUnknown
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func getDaemonKind() DaemonKind {
|
|
|
|
intValue, err := strconv.Atoi(os.Getenv(DaemonKindEnvKey))
|
|
|
|
if err != nil {
|
|
|
|
return DaemonKindUnknown
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
return DaemonKind(intValue)
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
// An Instruction is a command to be run by lazygit in daemon mode.
|
|
|
|
// It is serialized to json and passed to lazygit via environment variables
|
|
|
|
type Instruction interface {
|
|
|
|
Kind() DaemonKind
|
|
|
|
SerializedInstructions() string
|
|
|
|
|
|
|
|
// runs the instruction
|
|
|
|
run(common *common.Common) error
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func serializeInstruction[T any](instruction T) string {
|
|
|
|
jsonData, err := json.Marshal(instruction)
|
|
|
|
if err != nil {
|
|
|
|
// this should never happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(jsonData)
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func deserializeInstruction[T Instruction](jsonData string) Instruction {
|
|
|
|
var instruction T
|
|
|
|
err := json.Unmarshal([]byte(jsonData), &instruction)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return instruction
|
|
|
|
}
|
2023-03-29 00:49:19 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func ToEnvVars(instruction Instruction) []string {
|
|
|
|
return []string{
|
|
|
|
fmt.Sprintf("%s=%d", DaemonKindEnvKey, instruction.Kind()),
|
|
|
|
fmt.Sprintf("%s=%s", DaemonInstructionEnvKey, instruction.SerializedInstructions()),
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
2023-04-16 07:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExitImmediatelyInstruction struct{}
|
|
|
|
|
|
|
|
func (self *ExitImmediatelyInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindExitImmediately
|
|
|
|
}
|
2022-05-07 07:23:08 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *ExitImmediatelyInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ExitImmediatelyInstruction) run(common *common.Common) error {
|
2022-05-07 07:23:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func NewExitImmediatelyInstruction() Instruction {
|
|
|
|
return &ExitImmediatelyInstruction{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CherryPickCommitsInstruction struct {
|
|
|
|
Todo string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCherryPickCommitsInstruction(commits []*models.Commit) Instruction {
|
|
|
|
todoLines := lo.Map(commits, func(commit *models.Commit, _ int) TodoLine {
|
|
|
|
return TodoLine{
|
|
|
|
Action: "pick",
|
|
|
|
Commit: commit,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
todo := TodoLinesToString(todoLines)
|
|
|
|
|
|
|
|
return &CherryPickCommitsInstruction{
|
|
|
|
Todo: todo,
|
2023-04-05 19:01:49 +02:00
|
|
|
}
|
2023-04-16 07:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CherryPickCommitsInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindCherryPick
|
|
|
|
}
|
2023-04-02 03:37:02 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *CherryPickCommitsInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CherryPickCommitsInstruction) run(common *common.Common) error {
|
|
|
|
return handleInteractiveRebase(common, func(path string) error {
|
|
|
|
return utils.PrependStrToTodoFile(path, []byte(self.Todo))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChangeTodoActionsInstruction struct {
|
|
|
|
Changes []ChangeTodoAction
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangeTodoActionsInstruction(changes []ChangeTodoAction) Instruction {
|
|
|
|
return &ChangeTodoActionsInstruction{
|
|
|
|
Changes: changes,
|
2023-04-05 19:01:49 +02:00
|
|
|
}
|
2023-04-16 07:43:54 +02:00
|
|
|
}
|
2023-04-02 03:37:02 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *ChangeTodoActionsInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindChangeTodoActions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ChangeTodoActionsInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
2023-04-05 19:01:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *ChangeTodoActionsInstruction) run(common *common.Common) error {
|
|
|
|
return handleInteractiveRebase(common, func(path string) error {
|
|
|
|
for _, c := range self.Changes {
|
|
|
|
if err := utils.EditRebaseTodo(path, c.Sha, todo.Pick, c.NewAction); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-05 19:01:49 +02:00
|
|
|
}
|
2023-04-16 07:43:54 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type MoveTodoUpInstruction struct {
|
|
|
|
Sha string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMoveTodoUpInstruction(sha string) Instruction {
|
|
|
|
return &MoveTodoUpInstruction{
|
|
|
|
Sha: sha,
|
2023-04-02 03:37:02 +02:00
|
|
|
}
|
2023-04-16 07:43:54 +02:00
|
|
|
}
|
2023-04-02 03:37:02 +02:00
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *MoveTodoUpInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindMoveTodoUp
|
2023-04-02 03:37:02 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *MoveTodoUpInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MoveTodoUpInstruction) run(common *common.Common) error {
|
|
|
|
return handleInteractiveRebase(common, func(path string) error {
|
|
|
|
return utils.MoveTodoUp(path, self.Sha, todo.Pick)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type MoveTodoDownInstruction struct {
|
|
|
|
Sha string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMoveTodoDownInstruction(sha string) Instruction {
|
|
|
|
return &MoveTodoDownInstruction{
|
|
|
|
Sha: sha,
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *MoveTodoDownInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindMoveTodoDown
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 07:43:54 +02:00
|
|
|
func (self *MoveTodoDownInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MoveTodoDownInstruction) run(common *common.Common) error {
|
|
|
|
return handleInteractiveRebase(common, func(path string) error {
|
|
|
|
return utils.MoveTodoDown(path, self.Sha, todo.Pick)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type InsertBreakInstruction struct{}
|
|
|
|
|
|
|
|
func NewInsertBreakInstruction() Instruction {
|
|
|
|
return &InsertBreakInstruction{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *InsertBreakInstruction) Kind() DaemonKind {
|
|
|
|
return DaemonKindInsertBreak
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *InsertBreakInstruction) SerializedInstructions() string {
|
|
|
|
return serializeInstruction(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *InsertBreakInstruction) run(common *common.Common) error {
|
|
|
|
return handleInteractiveRebase(common, func(path string) error {
|
|
|
|
return utils.PrependStrToTodoFile(path, []byte("break\n"))
|
|
|
|
})
|
2022-05-07 07:23:08 +02:00
|
|
|
}
|