2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-12 10:04:20 +02:00
|
|
|
"fmt"
|
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 {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func NewRemoteCommands(gitCommon *GitCommon) *RemoteCommands {
|
2022-01-02 01:34:33 +02:00
|
|
|
return &RemoteCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("add", name, url).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("remove", name).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("rename", oldRemoteName, newRemoteName).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("remote").
|
|
|
|
Arg("set-url", remoteName, updatedUrl).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2021-10-24 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
func (self *RemoteCommands) DeleteRemoteBranch(task gocui.Task, remoteName string, branchName string) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("push").
|
|
|
|
Arg(remoteName, "--delete", branchName).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show-ref").
|
|
|
|
Arg("--verify", "--", fmt.Sprintf("refs/remotes/origin/%s", branchName)).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
_, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|