2018-07-12 13:44:46 +03:00
|
|
|
package commands
|
2018-07-12 10:55:13 +03:00
|
|
|
|
|
|
|
import (
|
2018-07-12 13:44:46 +03:00
|
|
|
"errors"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
"github.com/MichaelMure/git-bug/commands/completion"
|
|
|
|
"github.com/MichaelMure/git-bug/commands/execenv"
|
2019-03-27 23:44:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-07-12 10:55:13 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func newPullCommand() *cobra.Command {
|
2022-09-10 12:09:19 +03:00
|
|
|
env := execenv.NewEnv()
|
2020-06-28 19:26:29 +03:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "pull [REMOTE]",
|
2022-09-10 12:09:19 +03:00
|
|
|
Short: "Pull updates from a git remote",
|
|
|
|
PreRunE: execenv.LoadBackend(env),
|
|
|
|
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
|
2020-06-28 19:26:29 +03:00
|
|
|
return runPull(env, args)
|
2021-05-09 12:33:20 +03:00
|
|
|
}),
|
2022-09-10 12:09:19 +03:00
|
|
|
ValidArgsFunction: completion.GitRemote(env),
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func runPull(env *execenv.Env, args []string) error {
|
2018-07-12 10:55:13 +03:00
|
|
|
if len(args) > 1 {
|
2018-07-12 13:44:46 +03:00
|
|
|
return errors.New("Only pulling from one remote at a time is supported")
|
2018-07-12 10:55:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
remote := "origin"
|
|
|
|
if len(args) == 1 {
|
|
|
|
remote = args[0]
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Out.Println("Fetching remote ...")
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
stdout, err := env.Backend.Fetch(remote)
|
2018-09-07 18:10:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Out.Println(stdout)
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Out.Println("Merging data ...")
|
2018-09-07 18:10:40 +03:00
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
for result := range env.Backend.MergeAll(remote) {
|
2019-03-27 23:44:11 +03:00
|
|
|
if result.Err != nil {
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Err.Println(result.Err)
|
2018-09-07 18:10:40 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:44:11 +03:00
|
|
|
if result.Status != entity.MergeStatusNothing {
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Out.Printf("%s: %s\n", result.Id.Human(), result)
|
2018-09-07 18:10:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-07-12 10:55:13 +03:00
|
|
|
}
|