1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/remote.go

69 lines
1.6 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2020-09-29 12:03:39 +02:00
import (
"fmt"
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 03:32:27 +02:00
"github.com/jesseduffield/gocui"
2020-09-29 12:03:39 +02:00
)
2022-01-02 01:34:33 +02:00
type RemoteCommands struct {
*GitCommon
2022-01-02 01:34:33 +02:00
}
func NewRemoteCommands(gitCommon *GitCommon) *RemoteCommands {
2022-01-02 01:34:33 +02:00
return &RemoteCommands{
GitCommon: gitCommon,
2022-01-02 01:34:33 +02:00
}
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *RemoteCommands) AddRemote(name string, url string) error {
cmdArgs := NewGitCmd("remote").
Arg("add", name, url).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *RemoteCommands) RemoveRemote(name string) error {
cmdArgs := NewGitCmd("remote").
Arg("remove", name).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *RemoteCommands) RenameRemote(oldRemoteName string, newRemoteName string) error {
cmdArgs := NewGitCmd("remote").
Arg("rename", oldRemoteName, newRemoteName).
ToArgv()
return self.cmd.New(cmdArgs).Run()
2020-09-29 12:03:39 +02:00
}
2022-01-02 01:34:33 +02:00
func (self *RemoteCommands) UpdateRemoteUrl(remoteName string, updatedUrl string) error {
cmdArgs := NewGitCmd("remote").
Arg("set-url", remoteName, updatedUrl).
ToArgv()
return self.cmd.New(cmdArgs).Run()
}
func (self *RemoteCommands) DeleteRemoteBranch(task gocui.Task, remoteName string, branchName string) error {
cmdArgs := NewGitCmd("push").
Arg(remoteName, "--delete", branchName).
ToArgv()
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 03:32:27 +02:00
return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).WithMutex(self.syncMutex).Run()
2020-09-29 12:03:39 +02:00
}
// CheckRemoteBranchExists Returns remote branch
2022-01-02 01:34:33 +02:00
func (self *RemoteCommands) CheckRemoteBranchExists(branchName string) bool {
cmdArgs := NewGitCmd("show-ref").
Arg("--verify", "--", fmt.Sprintf("refs/remotes/origin/%s", branchName)).
ToArgv()
_, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
2020-09-29 12:03:39 +02:00
return err == nil
}