2022-01-08 14:00:36 +11:00
|
|
|
package git_commands
|
2020-09-29 20:03:39 +10:00
|
|
|
|
|
|
|
import (
|
2020-10-12 19:04:20 +11:00
|
|
|
"fmt"
|
2023-07-09 11:32:27 +10:00
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2020-09-29 20:03:39 +10:00
|
|
|
)
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
type RemoteCommands struct {
|
2022-01-18 21:26:21 +11:00
|
|
|
*GitCommon
|
2022-01-02 10:34:33 +11:00
|
|
|
}
|
|
|
|
|
2022-01-18 21:26:21 +11:00
|
|
|
func NewRemoteCommands(gitCommon *GitCommon) *RemoteCommands {
|
2022-01-02 10:34:33 +11:00
|
|
|
return &RemoteCommands{
|
2022-01-18 21:26:21 +11:00
|
|
|
GitCommon: gitCommon,
|
2022-01-02 10:34:33 +11:00
|
|
|
}
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
func (self *RemoteCommands) AddRemote(name string, url string) error {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("add", name, url).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-05-21 17:00:29 +10:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
func (self *RemoteCommands) RemoveRemote(name string) error {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("remove", name).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-05-21 17:00:29 +10:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
func (self *RemoteCommands) RenameRemote(oldRemoteName string, newRemoteName string) error {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("rename", oldRemoteName, newRemoteName).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-05-21 17:00:29 +10:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
func (self *RemoteCommands) UpdateRemoteUrl(remoteName string, updatedUrl string) error {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("set-url", remoteName, updatedUrl).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-05-21 17:00:29 +10:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2021-10-24 10:43:48 +11:00
|
|
|
}
|
|
|
|
|
2023-07-09 21:09:52 +10:00
|
|
|
func (self *RemoteCommands) DeleteRemoteBranch(task gocui.Task, remoteName string, branchName string) error {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("push").
|
|
|
|
Arg(remoteName, "--delete", branchName).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-07-09 11:32:27 +10:00
|
|
|
return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).WithMutex(self.syncMutex).Run()
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRemoteBranchExists Returns remote branch
|
2022-01-02 10:34:33 +11:00
|
|
|
func (self *RemoteCommands) CheckRemoteBranchExists(branchName string) bool {
|
2023-05-21 17:00:29 +10:00
|
|
|
cmdArgs := NewGitCmd("show-ref").
|
|
|
|
Arg("--verify", "--", fmt.Sprintf("refs/remotes/origin/%s", branchName)).
|
|
|
|
ToArgv()
|
2023-05-19 20:18:02 +10:00
|
|
|
|
2023-05-21 17:00:29 +10:00
|
|
|
_, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2020-09-29 20:03:39 +10:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|