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"
|
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 {
|
|
|
|
return self.cmd.
|
|
|
|
New(fmt.Sprintf("git remote add %s %s", self.cmd.Quote(name), self.cmd.Quote(url))).
|
2021-12-31 00:45:29 +02:00
|
|
|
Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *RemoteCommands) RemoveRemote(name string) error {
|
|
|
|
return self.cmd.
|
|
|
|
New(fmt.Sprintf("git remote remove %s", self.cmd.Quote(name))).
|
2021-12-31 00:45:29 +02:00
|
|
|
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 {
|
|
|
|
return self.cmd.
|
|
|
|
New(fmt.Sprintf("git remote rename %s %s", self.cmd.Quote(oldRemoteName), self.cmd.Quote(newRemoteName))).
|
2021-12-31 00:45:29 +02:00
|
|
|
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 {
|
|
|
|
return self.cmd.
|
|
|
|
New(fmt.Sprintf("git remote set-url %s %s", self.cmd.Quote(remoteName), self.cmd.Quote(updatedUrl))).
|
|
|
|
Run()
|
2021-10-24 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *RemoteCommands) DeleteRemoteBranch(remoteName string, branchName string) error {
|
|
|
|
command := fmt.Sprintf("git push %s --delete %s", self.cmd.Quote(remoteName), self.cmd.Quote(branchName))
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.cmd.New(command).PromptOnCredentialRequest().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 {
|
|
|
|
_, err := self.cmd.
|
2021-12-31 00:45:29 +02:00
|
|
|
New(
|
|
|
|
fmt.Sprintf("git show-ref --verify -- refs/remotes/origin/%s",
|
2022-01-02 01:34:33 +02:00
|
|
|
self.cmd.Quote(branchName),
|
2022-01-05 02:57:32 +02:00
|
|
|
),
|
|
|
|
).
|
|
|
|
DontLog().
|
2021-12-31 00:45:29 +02:00
|
|
|
RunWithOutput()
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|