Repository: jesseduffield/lazygit
File: pkg/app/daemon/daemon.go (Go)

Code before:
)

// 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
// 'LAZYGIT_DAEMON_KIND=EXIT_IMMEDIATELY' to specify that we want to run lazygit
// as a daemon which simply exits immediately. Any additional arguments we want
// to pass to a daemon can be done via other env vars.

type DaemonKind string

const (
	InteractiveRebase DaemonKind = "INTERACTIVE_REBASE"
	ExitImmediately   DaemonKind = "EXIT_IMMEDIATELY"
)

const (
	DaemonKindEnvKey   string = "LAZYGIT_DAEMON_KIND"
	RebaseTODOEnvKey   string = "LAZYGIT_REBASE_TODO"
	PrependLinesEnvKey string = "LAZYGIT_PREPEND_LINES"
)

type Daemon interface {
	Run() error
}

func Handle(common *common.Common) {
	d := getDaemon(common)
	if d == nil {
		return
	}

	if err := d.Run(); err != nil {
		log.Fatal(err)
	}

	os.Exit(0)
}

func InDaemonMode() bool {
	return getDaemonKind() != ""
}

func getDaemon(common *common.Common) Daemon {
	switch getDaemonKind() {
	case InteractiveRebase:
		return &rebaseDaemon{c: common}
	case ExitImmediately:
		return &exitImmediatelyDaemon{c: common}

Review comment (suggestion):
I'm not sure I'm following this code right, can you confirm:
When we want to do an interactive rebase (with a break) onto a ref, we pass `LAZYGIT_REBASE_TODO="break"` and `LAZYGIT_PREPEND_LINES=true` and then when the lazygit daemon is invoked to update the git-rebase-todo file, that file will already have all the commits from the original branch that we're about to rebase onto the ref, and we'll then prepend the break action onto the existing TODO lines so that in lazygit we can modify the remaining TODO lines. Is that right?

If that's correct, we should have a comment explaining what we're doing because it's not immediately obvious from looking at the code. We can put that comment above the `PrependLinesEnvKey` const

Code after:
)

// 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
// 'LAZYGIT_DAEMON_KIND=EXIT_IMMEDIATELY' to specify that we want to run lazygit
// as a daemon which simply exits immediately. Any additional arguments we want
// to pass to a daemon can be done via other env vars.

type DaemonKind string

const (
	InteractiveRebase DaemonKind = "INTERACTIVE_REBASE"
	ExitImmediately   DaemonKind = "EXIT_IMMEDIATELY"
)

const (
	DaemonKindEnvKey string = "LAZYGIT_DAEMON_KIND"
	RebaseTODOEnvKey string = "LAZYGIT_REBASE_TODO"

	// The `PrependLinesEnvKey` env variable is set to `true` to tell our daemon
	// to prepend the content of `RebaseTODOEnvKey` to the default `git-rebase-todo`
	// file instead of using it as a replacement.
	PrependLinesEnvKey string = "LAZYGIT_PREPEND_LINES"
)

type Daemon interface {
	Run() error
}

func Handle(common *common.Common) {
	d := getDaemon(common)
	if d == nil {
		return
	}

	if err := d.Run(); err != nil {
		log.Fatal(err)
	}

	os.Exit(0)
}

func InDaemonMode() bool {
	return getDaemonKind() != ""
}

func getDaemon(common *common.Common) Daemon {
	switch getDaemonKind() {