Repository: jesseduffield/lazygit
File: pkg/commands/git_commands/remote.go (Go)

Code before:
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

func (self *RemoteCommands) RenameRemote(oldRemoteName string, newRemoteName string) error {
	cmdArgs := NewGitCmd("remote").
		Arg("rename", oldRemoteName, newRemoteName).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

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, branchNames []string) error {
	cmdArgs := NewGitCmd("push").
		Arg(remoteName, "--delete").
		Arg(branchNames...).
		ToArgv()

	return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).Run()
}

func (self *RemoteCommands) DeleteRemoteTag(task gocui.Task, remoteName string, tagName string) error {
	cmdArgs := NewGitCmd("push").
		Arg(remoteName, "--delete", tagName).
		ToArgv()

	return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).Run()
}

// CheckRemoteBranchExists Returns remote branch
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()

	return err == nil
}

// Resolve what might be a aliased URL into a full URL
// SEE: `man -P 'less +/--get-url +n' git-ls-remote`

Review comment (suggestion):
I noticed that now we have two `lo.Map` calls, one here and one on the caller of `DeleteRemoteBranch`. Is it preferable to have these two joined together?

Also, the caller works with the type `models.RemoteBranch`, there is a `FullRefName()` method that returns `"refs/remotes/<remote_name>/<branch_name>"` which unfortunately can't be used for this, but on the tag side that works with `models.Tag` the `FullRefName()` can be used. It's a bit weird from the consistency standpoint so I don't know if we want to use them without some extra work, but I figured to just mention it.

Code after:
		Arg("remove", name).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

func (self *RemoteCommands) RenameRemote(oldRemoteName string, newRemoteName string) error {
	cmdArgs := NewGitCmd("remote").
		Arg("rename", oldRemoteName, newRemoteName).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

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, branchNames []string) error {
	cmdArgs := NewGitCmd("push").
		Arg(remoteName, "--delete").
		Arg(lo.Map(branchNames, func(b string, _ int) string { return "refs/heads/" + b })...).
		ToArgv()

	return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).Run()
}

func (self *RemoteCommands) DeleteRemoteTag(task gocui.Task, remoteName string, tagName string) error {
	cmdArgs := NewGitCmd("push").
		Arg(remoteName, "--delete", "refs/tags/"+tagName).
		ToArgv()

	return self.cmd.New(cmdArgs).PromptOnCredentialRequest(task).Run()
}

// CheckRemoteBranchExists Returns remote branch
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()

	return err == nil
}

// Resolve what might be a aliased URL into a full URL