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