2022-11-27 13:33:39 +00:00
|
|
|
package git
|
2019-06-07 10:32:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-02 21:21:35 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-02-08 09:16:10 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/execabs"
|
2019-06-07 10:32:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteRemove drops the defined remote from a git repo.
|
2023-02-08 09:16:10 +00:00
|
|
|
func RemoteRemove(repo Repository) *execabs.Cmd {
|
|
|
|
args := []string{
|
2019-06-07 10:32:16 +00:00
|
|
|
"remote",
|
|
|
|
"rm",
|
2022-12-02 21:21:35 +00:00
|
|
|
repo.RemoteName,
|
2023-02-08 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := execabs.Command(
|
|
|
|
gitBin,
|
|
|
|
args...,
|
2022-12-02 21:21:35 +00:00
|
|
|
)
|
|
|
|
cmd.Dir = repo.WorkDir
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-06-07 10:32:16 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAdd adds an additional remote to a git repo.
|
2023-02-08 09:16:10 +00:00
|
|
|
func RemoteAdd(repo Repository) *execabs.Cmd {
|
|
|
|
args := []string{
|
2019-06-07 10:32:16 +00:00
|
|
|
"remote",
|
|
|
|
"add",
|
2022-12-02 21:21:35 +00:00
|
|
|
repo.RemoteName,
|
|
|
|
repo.RemoteURL,
|
2023-02-08 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := execabs.Command(
|
|
|
|
gitBin,
|
|
|
|
args...,
|
2022-12-02 21:21:35 +00:00
|
|
|
)
|
|
|
|
cmd.Dir = repo.WorkDir
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-06-07 10:32:16 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-11-28 14:36:01 +00:00
|
|
|
// RemotePush pushs the changes from the local head to a remote branch.
|
2023-02-08 09:16:10 +00:00
|
|
|
func RemotePush(repo Repository) *execabs.Cmd {
|
|
|
|
args := []string{
|
2019-06-07 10:32:16 +00:00
|
|
|
"push",
|
2022-12-02 21:21:35 +00:00
|
|
|
repo.RemoteName,
|
|
|
|
fmt.Sprintf("HEAD:%s", repo.Branch),
|
2023-02-08 09:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := execabs.Command(
|
|
|
|
gitBin,
|
|
|
|
args...,
|
2022-12-02 21:21:35 +00:00
|
|
|
)
|
|
|
|
cmd.Dir = repo.WorkDir
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-06-07 10:32:16 +00:00
|
|
|
|
2022-12-02 21:21:35 +00:00
|
|
|
if repo.ForcePush {
|
2019-06-07 10:32:16 +00:00
|
|
|
cmd.Args = append(
|
|
|
|
cmd.Args,
|
2022-12-02 21:21:35 +00:00
|
|
|
"--force",
|
|
|
|
)
|
2019-06-07 10:32:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 21:21:35 +00:00
|
|
|
if repo.PushFollowTags {
|
2019-06-07 10:32:16 +00:00
|
|
|
cmd.Args = append(
|
|
|
|
cmd.Args,
|
|
|
|
"--follow-tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|